def __init__(self, model, camera_id, cascade_filename, run_local, wait=50): self.model = model self.wait = wait self.detector = CascadedDetector(cascade_fn=cascade_filename, minNeighbors=5, scaleFactor=1.1) if run_local: self.cam = create_capture(camera_id)
class Recognizer(object): def __init__(self, model, camera_id, cascade_filename, run_local, wait=50): self.model = model self.wait = wait self.detector = CascadedDetector(cascade_fn=cascade_filename, minNeighbors=5, scaleFactor=1.1) if run_local: self.cam = create_capture(camera_id) def run(self): while True: ret, frame = self.cam.read() # Resize the frame to half the original size for speeding up the detection process: # img = cv2.resize(frame, (frame.shape[1] / 2, frame.shape[0] / 2), interpolation=cv2.INTER_CUBIC) img = cv2.resize(frame, (320, 240), interpolation=cv2.INTER_CUBIC) imgout = img.copy() for i, r in enumerate(self.detector.detect(img)): x0, y0, x1, y1 = r # (1) Get face, (2) Convert to grayscale & (3) resize to image_size: face = img[y0:y1, x0:x1] face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) face = cv2.resize(face, self.model.image_size, interpolation=cv2.INTER_CUBIC) prediction = self.model.predict(face) predicted_label = prediction[0] classifier_output = prediction[1] # Now let's get the distance from the assuming a 1-Nearest Neighbor. # Since it's a 1-Nearest Neighbor only look take the zero-th element: distance = classifier_output['distances'][0] # Draw the face area in image: cv2.rectangle(imgout, (x0, y0), (x1, y1), (0, 0, 255), 2) # Draw the predicted name (folder name...): draw_str(imgout, (x0 - 20, y0 - 40), "Label " + self.model.subject_names[predicted_label]) draw_str(imgout, (x0 - 20, y0 - 20), "Feature Distance " + "%1.1f" % distance) cv2.imshow('OCVFACEREC < LOCAL', imgout) key = cv2.waitKey(self.wait) if 'q' == chr(key & 255): print ">> 'q' Pressed. Exiting." break
def __init__(self, model, cascade_filename, run_local, wait, rp): self.rp = rp self.wait = wait self.doRun = True self.model = model self.restart = False self.ros_restart_request = False self.detector = CascadedDetector(cascade_fn=cascade_filename, minNeighbors=5, scaleFactor=1.1) if run_local: print ">> Error: Run local selected in ROS based Recognizer" sys.exit(1) else: self.bridge = CvBridge() def signal_handler(signal, frame): print ">> ROS Exiting" self.doRun = False signal.signal(signal.SIGINT, signal_handler)
def __init__(self, model, camera_id, cascade_filename, run_local, inscope="/rsbopencv/ipl", outscope="/ocvfacerec/rsb/people", wait=50, notification="/ocvfacerec/rsb/restart/", show_gui=False): self.model = model self.wait = wait self.notification_scope = notification self.show_gui = show_gui self.detector = CascadedDetector(cascade_fn=cascade_filename, minNeighbors=5, scaleFactor=1.1) if run_local: print ">> Error Run local selected in RSB based Recognizer" sys.exit(1) self.doRun = True self.restart = False def signal_handler(signal, frame): print "\n>> RSB Exiting" self.doRun = False signal.signal(signal.SIGINT, signal_handler) # RSB try: rsb.converter.registerGlobalConverter(IplimageConverter()) except Exception, e: # If they are already loaded, the lib throws an exception ... # print ">> [Error] While loading RST converter: ", e pass