def findEyes(self): """ Detects eyes in a photo and initializes relevant attributes Uses opencv libarary methods to detect a face and then detect the eyes in that face. If there are exactly two eye regions found it populates the region attributes. If not exactly two eye regions are found the method returns false. Args: None Return: bool - True if there were no issues. False for any error """ #imcolor = cv.LoadImage(self.path) imcolor = self.facePhoto #Path setups cwd = os.path.dirname(os.path.abspath(sys.argv[0])) cwd += "/opencv/haarcascades/" frontalface = cwd + "haarcascade_frontalface_default.xml" eye = cwd + "haarcascade_eye.xml" faceCascade = cv.Load(frontalface) eyeCascade = cv.Load(eye) haarEyes = cv.Load(eye) storage = cv.CreateMemStorage() detectedEyes = cv.HaarDetectObjects(imcolor,haarEyes,storage) if DEBUG: print "detectedEyes = " + str(detectedEyes) if len(detectedEyes) == 2: if DEBUG: # Draw the rectangle cv.Rectangle(imcolor,(detectedEyes[0][0][0], detectedEyes[0][0][1]), (detectedEyes[0][0][0] + detectedEyes[0][0][2], detectedEyes[0][0][1] + detectedEyes[0][0][3]),cv.RGB(155,155,200),2) cv.Rectangle(imcolor,(detectedEyes[1][0][0], detectedEyes[1][0][1]), (detectedEyes[1][0][0] + detectedEyes[1][0][2], detectedEyes[1][0][1] + detectedEyes[1][0][3]),cv.RGB(155,155,200),2) cv.ShowImage("Face with eyes",imcolor) cv.WaitKey(0) cv.DestroyWindow("Face with eyes") left = (detectedEyes[0][0][0], detectedEyes[0][0][1], detectedEyes[0][0][0] + detectedEyes[0][0][2], detectedEyes[0][0][1] + detectedEyes[0][0][3]) right = (detectedEyes[1][0][0], detectedEyes[1][0][1], detectedEyes[1][0][0] + detectedEyes[1][0][2], detectedEyes[1][0][1] + detectedEyes[1][0][3]) if DEBUG: print "left: " + str(left) print "right: " + str(right) self.setEyes(left, right) return True if DEBUG: print "Found more or less than 2 eyes, returning false" return False
def draw_subdiv_facet(img, edge): t = edge count = 0 # count number of edges in facet while count == 0 or t != edge: count += 1 t = cv.Subdiv2DGetEdge(t, cv.CV_NEXT_AROUND_LEFT) buf = [] # gather points t = edge for i in range(count): assert t > 4 pt = cv.Subdiv2DEdgeOrg(t) if not pt: break buf.append((cv.Round(pt.pt[0]), cv.Round(pt.pt[1]))) t = cv.Subdiv2DGetEdge(t, cv.CV_NEXT_AROUND_LEFT) if (len(buf) == count): pt = cv.Subdiv2DEdgeDst(cv.Subdiv2DRotateEdge(edge, 1)) cv.FillConvexPoly( img, buf, cv.RGB(random.randrange(256), random.randrange(256), random.randrange(256)), cv.CV_AA, 0) cv.PolyLine(img, [buf], 1, cv.RGB(0, 0, 0), 1, cv.CV_AA, 0) draw_subdiv_point(img, pt.pt, cv.RGB(0, 0, 0))
def detect_face(self, input_image): # Equalize the histogram to reduce lighting effects search_image = cv2.equalizeHist(input_image) # Begin the search using three different XML template # First check one of the frontal templates if self.cascade_2: faces = self.cascade_2.detectMultiScale(search_image, **self.haar_params) # If that fails, check the profile template if not len(faces): if self.cascade_3: faces = self.cascade_3.detectMultiScale(search_image,**self.haar_params) # If that fails, check a different frontal profile if not len(faces): if self.cascade_1: faces = self.cascade_1.detectMultiScale(search_image, **self.haar_params) # If we did not detect any faces in this frame, display the message # "LOST FACE" on the marker image (defined in ros2opencv2.py) if not len(faces): self.last_face_box = None if self.show_text: font_face = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 cv2.putText(self.marker_image, "LOST FACE!", (20, int(self.frame_size[1] * 0.9)), font_face, font_scale, cv.RGB(255, 50, 50)) return None # If the show_text is set, display the hit rate so far if self.show_text: font_face = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 cv2.putText(self.marker_image, "Hit Rate: " + str(trunc(self.hit_rate, 2)), (20, int(self.frame_size[1] * 0.9)), font_face, font_scale, cv.RGB(255, 255, 0)) # If we do have a face, rescale it and publish for (x, y, w, h) in faces: # Set the face box to be cvRect which is just a tuple in Python face_box = (x, y, w, h) # If we have a face, publish the bounding box as the ROI if face_box is not None: self.ROI = RegionOfInterest() self.ROI.x_offset = min(self.frame_size[0], max(0, x)) self.ROI.y_offset = min(self.frame_size[1], max(0, y)) self.ROI.width = min(self.frame_size[0], w) self.ROI.height = min(self.frame_size[1], h) self.pubROI.publish(self.ROI) # Break out of the loop after the first face return face_box
def DetectRedEyes(image, faceCascade, eyeCascade): min_size = (20,20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 # Allocate the temporary images gray = cv.CreateImage((image.width, image.height), 8, 1) smallImage = cv.CreateImage((cv.Round(image.width / image_scale),cv.Round (image.height / image_scale)), 8 ,1) # Convert color input image to grayscale cv.CvtColor(image, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) face_region = cv.GetSubRect(image,(x,int(y + (h/4)),w,int(h/2))) cv.SetImageROI(image, (pt1[0], pt1[1], pt2[0] - pt1[0], int((pt2[1] - pt1[1]) * 0.7))) eyes = cv.HaarDetectObjects(image, eyeCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, (15,15)) if eyes: # For each eye found for eye in eyes: # Draw a rectangle around the eye cv.Rectangle(image, (eye[0][0], eye[0][1]), (eye[0][0] + eye[0][2], eye[0][1] + eye[0][3]), cv.RGB(255, 0, 0), 1, 8, 0) cv.ResetImageROI(image) return image
def DetectRedEyes(image, faceCascade): min_size = (20, 20) image_scale = 2 haar_scale = 1.1 min_neighbors = 2 haar_flags = 0 # Allocate the temporary images gray = cv.CreateImage((image.width, image.height), 8, 1) smallImage = cv.CreateImage((cv.Round( image.width / image_scale), cv.Round(image.height / image_scale)), 8, 1) # Convert color input image to grayscale cv.CvtColor(image, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces: #print faces for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints #print "face" global line2 line2 = n pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) # print pt1 # print pt2 cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 1, 8, 0) cv.PutText(image, "face" + str(h), pt1, font, cv.RGB(255, 0, 0)) cv.PutText(image, "Come close.", (0, 20), font, cv.RGB(255, 0, 0)) cv.PutText(image, "Ensure your forehead is well lit.", (0, 40), font, cv.RGB(255, 0, 0)) cv.PutText(image, "Hit escape when done.", (0, 60), font, cv.RGB(255, 0, 0)) cv.ResetImageROI(image) return image
def detect_and_draw(img, face_cascade): gray = cv.CreateImage((img.width, img.height), 8, 1) image_scale = img.width / smallwidth small_img = cv.CreateImage((cv.Round( img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1) # gray = cv.CreateImage((img.width,img.height), 8, 1) image_scale = img.width / smallwidth # small_img = cv.CreateImage((cv.Round(img.width / image_scale), cv.Round (img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) faces = cv.HaarDetectObjects(small_img, face_cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) if opencv_preview and faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) if verbose: print "Face at: ", pt1[0], ",", pt2[0], "\t", pt1[1], ",", pt2[ 1] return True if faces else False
def previewSideBySide(images): # scale the images down for display, and show them side by side # display a reticle over the images to help with convergence count = len(images) out = np.zeros((previewImageY, previewImageX*count, 3), dtype=np.uint8) i = 0 for img in images: scaled = cv2.resize(img, (previewImageX, previewImageY)) x = (previewImageX*i) out[0:previewImageY,x:x+previewImageX]=scaled cx = x + previewImageX/2 cv2.line(out, (cx, 0), (cx, previewImageY), cv.RGB(200,200,200), 1) i+=1 cv2.line(out, (0, previewImageY/2), (previewImageX*count, previewImageY/2), cv.RGB(200,200,200), 1) cv2.imshow('preview', out)
def get_image(camera, filename=None): im = cv.QueryFrame(camera) # take greyscale and compute RMS value im2 = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 3) cv.Convert(im, im2) gray = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 1) cv.CvtColor(im2, gray, cv.CV_RGB2GRAY) gray_mat = cv.GetMat(gray) img = numpy.asarray(gray_mat) power = numpy.sqrt(numpy.mean(img**2)) #save file if filename: font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2, cv.CV_AA) cv.PutText(im, filename, (DATE_X, DATE_Y), font, cv.RGB(255, 255, 0)) filename = os.path.join(DIR_PREFIX, filename + '.jpg') print filename cv.SaveImage(filename, im) del font else: filename = '' #del(camera) del im, im2, gray, img, gray_mat return (power, filename)
def depth_callback(self, data): # Convert the ROS image to OpenCV format using a cv_bridge helper function depth_image = self.convert_depth_image(data) # Process the depth image processed_depth_image = self.process_depth_image(depth_image) # Make global copies self.depth_image = depth_image.copy() self.processed_depth_image = processed_depth_image.copy() #self.publish_roi() font_face = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 cv2.imshow("depth", self.processed_depth_image) if (self.cX > 0) and (self.cY > 0): dist = depth_image[self.cY][self.cX + 10] # rospy.loginfo(str(dist)) cv2.putText( self.frame, "Distance:" + str(dist) + "cX=" + str(self.cX) + "cY" + str(self.cY), (10, 25), font_face, font_scale, cv.RGB(255, 255, 0)) self.dis_pub.publish(dist) self.keystroke = cv2.waitKey(5)
def detect_and_draw(img, cascade): # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img = cv.CreateImage((cv.Round(img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if(cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.)) if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) cv.ShowImage("result", img)
def detect_and_draw(self, img, cascade, camera_position=0): min_size = (20, 20) image_scale = self.horizontalSlider_3.value() haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img_height = cv.Round(img.height / image_scale) small_img = cv.CreateImage( (cv.Round(img.width / image_scale), small_img_height), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) if faces: for ((x, y, w, h), n) in faces: if self.face_cert < n: x2, y2, w2, h2 = self.make_the_rectangle_bigger( x, y, w, h, 1.22, small_img_height, image_scale) self.create_person_and_add_to_room(img, (x2, y2, w2, h2), camera_position) if self.mark_detected_objects[camera_position]: pt2 = (int(x2 + w2), int(y2 + h2)) cv.Rectangle(img, (x2, y2), pt2, cv.RGB(255, 0, 0), 3, 8, 0) if self.show_main_view[camera_position]: cv.ShowImage("result" + str(camera_position), img)
def hs_histogram(src): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) # Extract the H and S planes h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane] h_bins = 30 s_bins = 32 hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) s_ranges = [0, 255] ranges = [h_ranges, s_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) (_, max_value, _, _) = cv.GetMinMaxHistValue(hist) hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3) for h in range(h_bins): for s in range(s_bins): bin_val = cv.QueryHistValue_2D(hist, h, s) intensity = cv.Round(bin_val * 255 / max_value) cv.Rectangle(hist_img, (h * scale, s * scale), ((h + 1) * scale - 1, (s + 1) * scale - 1), cv.RGB(intensity, intensity, intensity), cv.CV_FILLED) return hist_img
def DetectFace(image, faceCascade, returnImage=False): # This function takes a grey scale cv image and finds # the patterns defined in the haarcascade function # modified from: http://www.lucaamore.com/?p=638 #variables min_size = (30, 30) #image_scale = 2 haar_scale = 1.1 min_neighbors = 2 haar_flags = 0 # Equalize the histogram cv.EqualizeHist(image, image) # Detect the faces faces = cv.HaarDetectObjects(image, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces and returnImage: for ((x, y, w, h), n) in faces: # Convert bounding box to two CvPoints pt1 = (int(x), int(y)) pt2 = (int(x + w), int(y + h)) cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0) if returnImage: return image else: return faces
def add_keypoints(self, roi, ignore_distance=False): # Begin with a mask of all black pixels mask = np.zeros_like(self.grey) # Get the coordinates and dimensions of the currently ROI try: ((x, y), (w, h), a) = roi except: try: x, y, w, h = roi a = 0 except: rospy.loginfo("ROI has shrunk to zero...") return x = int(x) y = int(y) # Expand the ROI to look for new keypoints w_new = int(self.expand_roi * w) h_new = int(self.expand_roi * h) pt1 = (x - int(w_new / 2), y - int(h_new / 2)) pt2 = (x + int(w_new / 2), y + int(h_new / 2)) mask_box = ((x, y), (w_new, h_new), a) # Display the expanded ROI with a yellow rectangle if self.show_add_drop: cv2.rectangle(self.marker_image, pt1, pt2, cv.RGB(255, 255, 0)) # Create a filled white ellipse within the mask box to define the ROI cv2.ellipse(mask, mask_box, cv.CV_RGB(255, 255, 255), cv.CV_FILLED) if self.keypoints is not None: # Mask the current keypoints for x, y in [np.int32(p) for p in self.keypoints]: cv2.circle(mask, (x, y), 5, 0, -1) new_keypoints = cv2.goodFeaturesToTrack(self.grey, mask=mask, **self.gf_params) # Append new keypoints to the current list if they are not # too far from the current cluster if new_keypoints is not None: for x, y in np.float32(new_keypoints).reshape(-1, 2): if ignore_distance: self.keypoints.append((x, y)) else: distance = self.distance_to_cluster((x, y), self.keypoints) if distance > self.add_keypoint_distance: self.keypoints.append((x, y)) # Briefly display a blue disc where the new point is added if self.show_add_drop: cv2.circle(self.marker_image, (x, y), 3, (255, 255, 0, 0), cv.CV_FILLED, 2, 0) # Remove duplicate keypoints self.keypoints = list(set(self.keypoints))
def detectFace(img, cascade): # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img = cv.CreateImage( (cv.Round(img.width / imageScale), cv.Round(img.height / imageScale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haarScale, minNeighbors, haarFlags, minSize) if faces: print "\tDetected ", len(faces), " object(s)" for ((x, y, w, h), n) in faces: #the input to cv.HaarDetectObjects was resized, scale the #bounding box of each face and convert it to two CvPoints pt1 = (int(x * imageScale), int(y * imageScale)) pt2 = (int((x + w) * imageScale), int((y + h) * imageScale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) return img else: return False
class Color: black = cv.RGB(0, 0, 0) white = cv.RGB(255, 255, 255) red = cv.RGB(255, 0, 0) green = cv.RGB(0, 255, 0) blue = cv.RGB(0, 0, 255) cyan = cv.RGB(0, 255, 255) magenta = cv.RGB(255, 0, 255) yellow = cv.RGB(255, 255, 0)
def detect_and_draw(img, cascade, jpg_cnt): # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img = cv.CreateImage((cv.Round( img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if (cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t print "detection time = %gms" % (t / (cv.GetTickFrequency() * 10000)) if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) if jpg_cnt % 50 == 1: print('capture completed') cv.SaveImage('test_' + str(jpg_cnt) + '.jpg', img) print("aaa1") url = 'http://210.94.185.52:8080/upload.php' #files={ 'upfiles' : open('/home/lee/test_'+str(jpg_cnt)+'.jpg','rb')} files = { 'upfiles': open('/home/lee/test_' + str(jpg_cnt) + '.jpg', 'rb') } print("aaa2") r = requests.post(url, files=files) print("aaa3") print(r.text) for i in r.text.split(): try: op = float(i) break except: continue print(op) #LED if op >= 0.9: lock_on() else: print('no') cv.ShowImage("result", img)
def draw_Lines(lines, img): for (rho, theta) in lines[:5]: a = cos(theta) b = sin(theta) x0 = a * rho y0 = b * rho pt1 = (cv.Round(x0 + 1000 * (-b)), cv.Round(y0 + 1000 * (a))) pt2 = (cv.Round(x0 - 1000 * (-b)), cv.Round(y0 - 1000 * (a))) cv.Line(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8)
def detect_and_draw(img, cascade, detected): # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) image_scale = img.width / smallwidth small_img = cv.CreateImage((cv.Round( img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if (cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # t = cv.GetTickCount() - t # print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.)) if faces: if detected == 0: # os.system('festival --tts hi &') detected = 1 for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) print "Face at: ", pt1[0], ",", pt2[0], "\t", pt1[1], ",", pt2[ 1] # find amount needed to pan/tilt span = (pt1[0] + pt2[0]) / 2 stlt = (pt1[1] + pt2[1]) / 2 mid = smallwidth / 2 if span < mid: print "left", mid - span else: print "right", span - mid #os.system('echo "6="' + str(valTilt) + ' > /dev/pi-blaster') #os.system('echo "7="' + str(valPan) + ' > /dev/pi-blaster') else: if detected == 1: #print "Last seen at: ", pt1[0], ",", pt2[0], "\t", pt1[1], ",", pt2[1] #os.system('festival --tts bye &') status = "just disappeared" detected = 0 cv.ShowImage("result", img) return detected
def detect_and_draw(img, cascade, c): # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img = cv.CreateImage((cv.Round( img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) face_flag = False if (cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.)) if faces: face_flag = True for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) # ある程度顔が検出されたら if c > 4: # 画像の保存 global counter counter = -1 d = datetime.today() datestr = d.strftime('%Y-%m-%d_%H-%M-%S') outputname = '/home/pi/fd/fd_' + datestr + '.jpg' cv.SaveImage(outputname, img) print 'Face Detect' # 読み込みと切り取り fimg = cv.LoadImage(outputname) fimg_trim = fimg[pt1[1]:pt2[1], pt1[0]:pt2[0]] outputname2 = '/home/pi/fd/face_' + datestr + '.jpg' cv.SaveImage(outputname2, fimg_trim) print 'Face Image Save' cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) cv.ShowImage("result", img) return face_flag
def detect_and_draw(img, cascade): # allocate temporary images gray = cv.CreateImage((img.width,img.height), 8, 1) small_img = cv.CreateImage((cv.Round(img.width / image_scale), cv.Round (img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if(cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t #print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.)) if faces: count = 0 stop = 1 name = 1 no = 1 dict = {} for num in range(14): dict[name] = no name += 1 print dict f = open('no.json','w') json.dump(dict,f) #for count in range(14): #time.sleep(stop) #count += 1 #print(count) #time.sleep(stop) #cv.PutText(img, "SAMPLE_TEXT", (0, 50), cv.CV_FONT_HERSHEY_PLAIN, cv.RGB(255, 255, 255)) #cv.PutText(img, "SAMPLE_TEXT", (0, 50), cv.CV_FONT_HERSHEY_PLAIN, 4, (255, 255, 255), 2, cv.CV_AA ) for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints #for count in range(14): count += 1 print(count) pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) #count = count + 1 #print(count) # cv.putText(img, "SAMPLE_TEXT", (0, 50), FONT_HERSHEY_PLAIN, 4, (255, 255, 255), 2, cv.CV_AA) cv.ShowImage("result", img)
def detect_face(self, input_image): # First check one of the frontal templates if self.cascade_1: faces = self.cascade_1.detectMultiScale(input_image, **self.haar_params) # If that fails, check the profile template if len(faces) == 0 and self.cascade_3: faces = self.cascade_3.detectMultiScale(input_image, **self.haar_params) # If that also fails, check a the other frontal template if len(faces) == 0 and self.cascade_2: faces = self.cascade_2.detectMultiScale(input_image, **self.haar_params) # The faces variable holds a list of face boxes. # If one or more faces are detected, return the first one. if len(faces) > 0: face_box = faces[0] else: # If no faces were detected, print the "LOST FACE" message on the screen if self.show_text: font_face = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 cv2.putText(self.marker_image, "LOST FACE!", (int( self.frame_size[0] * 0.65), int(self.frame_size[1] * 0.9)), font_face, font_scale, cv.RGB(255, 50, 50)) face_box = None # Display the hit rate so far if self.show_text: font_face = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 cv2.putText(self.marker_image, "Hit Rate: " + str(trunc(self.hit_rate, 2)), (20, int(self.frame_size[1] * 0.9)), font_face, font_scale, cv.RGB(255, 255, 0)) return face_box
def detect_and_draw(img, cascade): # allocate temporary images gray = cv.CreateImage((img.width,img.height), 8, 1) small_img = cv.CloneMat(img)# cv.CreateImage((img.width,img.height)) # (cv.Round(img.width / image_scale),cv.Round (img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if(cascade): t = cv.GetTickCount() #Scan image and get an array of faces faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t #print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.)) if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) print "X " , x if int(x * image_scale) > (img.width * 0.45): #print "X " , x #print steppera.IsTurning() if (steppera.IsTurning() == False): if (stepperInUse[STEPPERA] == True): sensor_value = "-4" if isNumeric(sensor_value): print "Moving to" , sensor_value steppera.changeSpeed(int(100 * sign(int(float(sensor_value)) - 0)),abs(int(float(sensor_value)) - 0)) while (steppera.IsTurning() == True): cv.WaitKey(100) if int((x + w) * image_scale) < (img.width * 0.55): #print "X " , x #print steppera.IsTurning() if (steppera.IsTurning() == False): if (stepperInUse[STEPPERA] == True): sensor_value = "4" if isNumeric(sensor_value): print "Moving to" , sensor_value steppera.changeSpeed(int(100 * sign(int(float(sensor_value)) - 0)),abs(int(float(sensor_value)) - 0)) while (steppera.IsTurning() == True): cv.WaitKey(100) cv.ShowImage("result", img)
def drawPlayer(x, y, hm, img, r, g, b): tz = hm[2, 0] * x + hm[2, 1] * y + hm[2, 2] tx = hm[0, 0] * x + hm[0, 1] * y + hm[0, 2] ty = hm[1, 0] * x + hm[1, 1] * y + hm[1, 2] tx = int(tx / tz) ty = int(ty / tz) cv2.circle(img, (tx - 2503, ty), 20, cv.RGB(r, g, b), thickness=cv.CV_FILLED, lineType=8, shift=0) return img, tx - 2503, ty
def previewAlternate(images): # alternate left and right photos global pframe count = len(images) out = np.zeros((previewImageY, previewImageX, 3), dtype=np.uint8) i = 0 if stereo: img = images[pframe&1] pframe+=1 else: img = images[0] scaled = cv2.resize(img, (previewImageX, previewImageY)) x = (previewImageX*i) out[0:previewImageY,x:x+previewImageX]=scaled cx = x + previewImageX/2 cv2.line(out, (cx, 0), (cx, previewImageY), cv.RGB(200,200,200), 1) cv2.line(out, (0, previewImageY/2), (previewImageX*count, previewImageY/2), cv.RGB(200,200,200), 1) cv2.imshow('preview', out)
def detect_and_draw(img, cascade): # Acondicionamiento de la imagen, imagenes temporales gray = cv.CreateImage((img.width,img.height), 8, 1) small_img = cv.CreateImage((cv.Round(img.width / image_scale), cv.Round (img.height / image_scale)), 8, 1) # Crea la mascara cv.CvtColor(img, gray, cv.CV_BGR2GRAY) #Escala la imagen para procesarla homogeneamente #Aplicacion de la Imagen Integral cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) # PROcesamiento de la Imagen if(cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t print "Tiempo de la deteccion = %gms" % (t/(cv.GetTickFrequency()*1000.)) if faces: for ((x, y, w, h), n) in faces: #calcula los puntos de deteccion pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) posx = int(((x+(x+w))/2)* image_scale) posy = int(((y+(y+h))/2)* image_scale) #dibuja un circulo en el centro de la imagen y otro en el centro del rostro cv.Circle(img, (80,60), 5, cv.RGB(0,0,255), 1, 8, 0) cv.Circle(img, (posx, posy), 5, cv.RGB(0,255,0), 1, 8, 0) #si el estado esta libre enviamos datos de posicion por i2c state = readData() time.sleep(0.005) if state == 1: sendData(posx) sendData(posy) print 'posx: ' + str(posx) + ' posy: ' + str(posy) cv.ShowImage("video", img)
def getMouth2(imcolor,detectedFace,storage=None,DEBUG=None): if not storage: storage = cv.CreateMemStorage() # loading the classifiers haarMouth = cv.Load(defaults.haar_mouth) # running the classifiers detectedMouth = cv.HaarDetectObjects(imcolor, haarMouth, storage) # draw a green rectangle where the face is detected if DEBUG and detectedMouth: for mouth in detectedMouth: showRectangle(imcolor,eye,cv.RGB(155, 10, 110)) return detectedMouth
def veido_atpazinimas(self, frame): """Veido atpazinimo metodas""" frame = cv.fromarray(frame) # running the classifiers detectedFace = cv.HaarDetectObjects(frame, haarFace, storage) if detectedFace: for face in detectedFace: cv.Rectangle(frame,(face[0][0],face[0][1]), (face[0][0]+face[0][2],face[0][1]+face[0][3]), cv.RGB(155, 255, 25),2) frame = numpy.asarray(frame) return frame
def test_2542670(self): xys = [(94, 121), (94, 122), (93, 123), (92, 123), (91, 124), (91, 125), (91, 126), (92, 127), (92, 128), (92, 129), (92, 130), (92, 131), (91, 132), (90, 131), (90, 130), (90, 131), (91, 132), (92, 133), (92, 134), (93, 135), (94, 136), (94, 137), (94, 138), (95, 139), (96, 140), (96, 141), (96, 142), (96, 143), (97, 144), (97, 145), (98, 146), (99, 146), (100, 146), (101, 146), (102, 146), (103, 146), (104, 146), (105, 146), (106, 146), (107, 146), (108, 146), (109, 146), (110, 146), (111, 146), (112, 146), (113, 146), (114, 146), (115, 146), (116, 146), (117, 146), (118, 146), (119, 146), (120, 146), (121, 146), (122, 146), (123, 146), (124, 146), (125, 146), (126, 146), (126, 145), (126, 144), (126, 143), (126, 142), (126, 141), (126, 140), (127, 139), (127, 138), (127, 137), (127, 136), (127, 135), (127, 134), (127, 133), (128, 132), (129, 132), (130, 131), (131, 130), (131, 129), (131, 128), (132, 127), (133, 126), (134, 125), (134, 124), (135, 123), (136, 122), (136, 121), (135, 121), (134, 121), (133, 121), (132, 121), (131, 121), (130, 121), (129, 121), (128, 121), (127, 121), (126, 121), (125, 121), (124, 121), (123, 121), (122, 121), (121, 121), (120, 121), (119, 121), (118, 121), (117, 121), (116, 121), (115, 121), (114, 121), (113, 121), (112, 121), (111, 121), (110, 121), (109, 121), (108, 121), (107, 121), (106, 121), (105, 121), (104, 121), (103, 121), (102, 121), (101, 121), (100, 121), (99, 121), (98, 121), (97, 121), (96, 121), (95, 121)] #xys = xys[:12] + xys[16:] pts = cv.CreateMat(len(xys), 1, cv.CV_32SC2) for i,(x,y) in enumerate(xys): pts[i,0] = (x, y) storage = cv.CreateMemStorage() hull = cv.ConvexHull2(pts, storage) hullp = cv.ConvexHull2(pts, storage, return_points = 1) defects = cv.ConvexityDefects(pts, hull, storage) vis = cv.CreateImage((1000,1000), 8, 3) x0 = min([x for (x,y) in xys]) - 10 x1 = max([x for (x,y) in xys]) + 10 y0 = min([y for (y,y) in xys]) - 10 y1 = max([y for (y,y) in xys]) + 10 def xform(pt): x,y = pt return (1000 * (x - x0) / (x1 - x0), 1000 * (y - y0) / (y1 - y0)) for d in defects[:2]: cv.Zero(vis) # First draw the defect as a red triangle cv.FillConvexPoly(vis, [xform(p) for p in d[:3]], cv.RGB(255,0,0)) # Draw the convex hull as a thick green line for a,b in zip(hullp, hullp[1:]): cv.Line(vis, xform(a), xform(b), cv.RGB(0,128,0), 3) # Draw the original contour as a white line for a,b in zip(xys, xys[1:]): cv.Line(vis, xform(a), xform(b), (255,255,255)) self.snap(vis)
def getFaces(imcolor,storage=None,DEBUG=None): if not storage: storage = cv.CreateMemStorage() # loading the classifiers haarFace = cv.Load(defaults.haar_face) # running the classifiers detectedFace = cv.HaarDetectObjects(imcolor, haarFace, storage) # draw a green rectangle where the face is detected if DEBUG and detectedFace: for face in detectedFace: showRectangle(imcolor,face,cv.RGB(155, 55, 200)) return detectedFace