def start(self): cv2.namedWindow("opening...") vc = cv2.VideoCapture(0) detect_flag = False videoflag = 0 rval, frame = vc.read() s = time.time() a = time.time() tuples = [] # list of (image_ID, face, tag) self.faces = [] while True: if frame is not None: pframe = cv2.flip(cv2.resize(frame, (960, 720)), 1) dframe = cv2.resize(pframe, (480, 360)) # size ratio = 4 ratio = 2 p = Photo() faces = detect_faces(self.detector, dframe) faces = sorted(faces, key=lambda tup: tup[0]) for (r_x, r_y, r_w, r_h) in faces: x, y, w, h = ratio*r_x, ratio*r_y, ratio*r_w, ratio*r_h p.add_face(x, y, w, h) # Draw a rectangle around the faces cv2.rectangle(pframe, (x, y), (x+w, y+h), (255, 0, 0), 2) p.image = pframe p.extract_features() self.faces = [] if len(p.faces) != 0: X = np.array([f.feature for f in p.faces]) predY = self.model.predict(X) for (i, f) in enumerate(p.faces): if inv_label_maps[predY[i]] == 'T': cv2.rectangle(pframe, (f.x, f.y), (f.x+f.w, f.y+f.h), (0, 255, 0), 2) self.faces += [(f.x, f.y, f.w, f.h, inv_label_maps[predY[i]], None)] cv2.imshow("camera", pframe) rval, frame = vc.read() oper = cv2.waitKey(1) & 0xFF if oper == ord('p'): t = time.time() iname = datetime.datetime.fromtimestamp(t).strftime('%Y-%m-%d %H.%M.%S') print iname #iname = "/Users/GDog/Desktop/" + iname + ".png" self.images = pframe vc.release() cv2.destroyAllWindows() break
def start(): cv2.namedWindow("preview") vc = cv2.VideoCapture(0) videoflag = 0 rval, frame = vc.read() s = time.time() a = time.time() while True: if frame is not None: pframe = cv2.flip(cv2.resize(frame, (960, 720)), 1) p = Photo() faces = detect_faces(detector, pframe) faces = sorted(faces, key=lambda tup: tup[0]) for (x, y, w, h) in faces: p.add_face(x, y, w, h) # Draw a rectangle around the faces cv2.rectangle(pframe, (x, y), (x+w, y+h), (255, 0, 0), 2) p.image = pframe p.extract_features() if len(p.faces) != 0: X = np.array([f.feature for f in p.faces]) predY = model.predict(X) for (i, f) in enumerate(p.faces): if inv_label_maps[predY[i]] == 'T': cv2.rectangle(pframe, (f.x, f.y), (f.x+f.w, f.y+f.h), (0, 255, 0), 2) cv2.imshow("image", pframe) rval, frame = vc.read() oper = cv2.waitKey(1) & 0xFF if oper == ord('q'): break if oper == ord('p'): t = time.time() iname = datetime.datetime.fromtimestamp(t).strftime('%Y-%m-%d %H.%M.%S') print iname #iname = "/Users/GDog/Desktop/" + iname + ".png" self.images += [pframe] vc.release() break
videoflag = 0 rval, frame = vc.read() s = time.time() a = time.time() while True: if frame is not None: #print frame.shape print "image extract:", time.time()-a a = time.time() pframe = cv2.flip(cv2.resize(frame, (960, 720)), 1) print "flip+resize: ", time.time()-a a = time.time() #detector part p = Photo() faces = detect_faces(detector, pframe) faces = sorted(faces, key=lambda tup: tup[0]) for (x, y, w, h) in faces: p.add_face(x, y, w, h) # Draw a rectangle around the faces cv2.rectangle(pframe, (x, y), (x+w, y+h), (255, 0, 0), 2) print "detect: ", time.time()-a a = time.time() #filter part p.image = pframe p.extract_features() if len(p.faces) != 0: X = np.array([f.feature for f in p.faces]) predY = model.predict(X)
from utils import detect_faces from datasets import Photo, inv_label_maps if __name__ == "__main__": print "USAGE : python predict.py model_path (-d image_predfix)/(image_path) " model_path = sys.argv[1] if sys.argv[2] == "-d": image_prefix = sys.argv[3] image_list = [image_path for image_path in sorted(glob.glob(image_prefix + "/*.[Jj][Pp][Gg]"))] else: image_list = [sys.argv[2]] with open(model_path, "rb") as f: model = cPickle.load(f) for image_path in image_list: p = Photo() # before applying the filter image1 = cv2.imread(image_path) image1 = cv2.resize(image1, (960, 720)) if image1 is None: print "no image", image_path continue detector = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml") faces = detect_faces(detector, image1) print "find %d faces in image %s " % (len(faces), image_path) faces = sorted(faces, key=lambda tup: tup[0]) for (x, y, w, h) in faces: p.add_face(x, y, w, h) # Draw a rectangle around the faces cv2.rectangle(image1, (x, y), (x + w, y + h), (0, 255, 0), 2)