Skip to content

Commit

Permalink
New programs implemented
Browse files Browse the repository at this point in the history
* README.md: Corresponding introductions added
* display.py: Optimized in argument parsing
* gen_encoding.py: Optimized
* display_faces.py: Created for putting rectangles on faces
* paint_stuff.py: Allow users to paint green rectangles on pictures

Here are why and how to optimize argument parsing in Python:
As we know, we load a number of modules to in order to simplify the
procedure, but loading process is time-consuming. But Python allows
us to import modules whenever we want. Therefore I let the program
parse the arguments first before loading other large modules, and this
is a time-saving solution for invalid command line argument.

Just a note, though TL;DR
  • Loading branch information
TravorLZH committed Jun 23, 2019
1 parent 2a95e15 commit 8fe7e63
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ $ pip3 install face_recognition # This installs dlib as its dependency
* **mirror\_facedetect.py** to detect faces in the webcam
* **face\_ident.py** to identify the actual person from their faces
* **gen\_encoding.py** to generate `.face` files from images
* **display\_faces.py** to put rectangles on the faces of a picture
* **paint\_stuff.py** to draw rectangles by mouse on a custom background image
9 changes: 7 additions & 2 deletions display.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#!/usr/bin/env python3
import sys

argc=len(sys.argv)

if argc<2:
print("usage: %s image_file" % sys.argv[0])
exit(-1)

import cv2
import numpy

assert(len(sys.argv)>=2)

# Load the image file into an ndarray
image=cv2.imread(sys.argv[1],cv2.IMREAD_UNCHANGED);

Expand Down
22 changes: 22 additions & 0 deletions display_faces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3
import sys

argc=len(sys.argv)

if argc<2:
print("usage: %s image_file" % sys.argv[0])
exit(-1)

import face_recognition as fr
import numpy as np
import cv2

image=fr.load_image_file(sys.argv[1]) # This is for face detection
cvimg=cv2.imread(sys.argv[1],cv2.IMREAD_UNCHANGED)
faces=fr.face_locations(image)

for top,right,bottom,left in faces:
cvimg=cv2.rectangle(cvimg,(left,top),(right,bottom),(0,0xFF,0),3)

cv2.imshow("Results from face detector",cvimg)
cv2.waitKey(0)
8 changes: 4 additions & 4 deletions gen_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
print("usage: %s input_file [output_file]" % sys.argv[0])
exit(-1)

import numpy as np
import face_recognition as fr
import csv

input_filename=sys.argv[1]
output_filename=""
if argc>=3:
Expand All @@ -22,6 +18,10 @@
output_filename='.'.join(parts)
print("output data to `%s'" % output_filename)

import numpy as np
import face_recognition as fr
import csv

image=fr.load_image_file(input_filename)
encoding=fr.face_encodings(image)[0]
# Now write the data to the output file
Expand Down
44 changes: 44 additions & 0 deletions paint_stuff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
import sys

argc=len(sys.argv)

if argc<2:
print("usage: %s background_image" % sys.argv[0])
exit(-1)

img_fname=sys.argv[1]

import cv2
import numpy as np

x0=0
y0=0
flag=False

img=cv2.imread(img_fname,cv2.IMREAD_UNCHANGED)

win="~Put some rectangles~"

def mouse_handler(event,x,y,flags,param):
global x0,y0,flag
if event==cv2.EVENT_LBUTTONDOWN:
print("Start drawing")
flag=True
x0=x
y0=y
elif event==cv2.EVENT_MOUSEMOVE:
if flag:
tmp=np.array(img,copy=True) # Make a copy from it
cv2.rectangle(tmp,(x0,y0),(x,y),(0,0xFF,0),3)
cv2.imshow(win,tmp)
elif event==cv2.EVENT_LBUTTONUP:
flag=False
print("Rectangle between (%d,%d) and (%d,%d)" % (x0,y0,x,y))
cv2.rectangle(img,(x0,y0),(x,y),(0,0xFF,0),3)
cv2.imshow(win,img)

cv2.namedWindow(win)
cv2.setMouseCallback(win,mouse_handler)
cv2.imshow(win,img)
cv2.waitKey(0)

0 comments on commit 8fe7e63

Please sign in to comment.