def find_lines_in_map_probabilistic(self, map_img): #Finds lines in the image using the probabilistic hough transform lines=cv.CreateMemStorage(0) line_img=cv.CreateMat(map_img.height, map_img.width, cv.CV_8UC1) cv.Set(line_img, 255) lines = cv.HoughLines2(map_img,cv.CreateMemStorage(), cv.CV_HOUGH_PROBABILISTIC, self.hough_rho,np.pi/2,self.hough_threshold) np_line=np.asarray(lines) print "list of probabilistic lines: ", np_line
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 detect_and_draw(img, front_cascade, profile_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(front_cascade): # Test for frontal face faces = cv.HaarDetectObjects(small_img, front_cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) if faces: # we've detected a face return [faces, FRONTAL] # Test for profile face faces = cv.HaarDetectObjects(small_img, profile_cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) if faces: # we've detected a face return [faces, PROFILE] #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)) #imgWidth, imgHeight = cv.GetSize(img) #croppedX = max(0, x*image_scale-w*image_scale/2) #croppedY = max(0, y*image_scale-h*image_scale/2) #croppedW = min(imgWidth, (2*w)*image_scale) #croppedH = min(imgHeight, (2*h)*image_scale) #imgCropped = cv.CreateImage((croppedW, croppedH), img.depth, img.nChannels) #srcRegion = cv.GetSubRect(img, (croppedX, croppedY, croppedW, croppedH)) #cv.Copy(srcRegion, imgCropped) #cv.ShowImage("cropped", imgCropped) #cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) return []
def ifFace(img,size): gray=cv.CreateImage(size,8,1) cv.CvtColor(img,gray,cv.CV_BGR2GRAY) newMem1=cv.CreateMemStorage(0) newMem2=cv.CreateMemStorage(0) newMem3=cv.CreateMemStorage(0) cv.EqualizeHist(gray,gray) face=cv.HaarDetectObjects(gray,c_f,newMem1,1.2,3,cv.CV_HAAR_DO_CANNY_PRUNING,(50,50)) mouth=cv.HaarDetectObjects(gray,c_m,newMem2,1.2,2,cv.CV_HAAR_DO_CANNY_PRUNING,(10,10)) body=cv.HaarDetectObjects(gray,c_m,newMem3,1.2,2,cv.CV_HAAR_DO_CANNY_PRUNING,(100,100)) if face and mouth or body: cv.SaveImage("img/out.jpg",img) return 1 else: return 0
def detectRightEye(self, img, rightEyeArea, centerX, centerY, pt1, cascade2): rightEye = cv.HaarDetectObjects(rightEyeArea, cascade2, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # in case of multiple find the maximum box minArea = 0 pt3 = (0, 0) pt4 = (0, 0) if rightEye: for ((x, y, w, h), n) in rightEye: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints if(w * h > minArea): minArea = w * h pt3 = (x, y) pt4 = (x + w, y + h) # pt3 = (int(x * image_scale), int(y * image_scale)) # pt4 = (int((x + w) * image_scale), int((y + h) * image_scale)) # print "point 3 " + str(pt3) # print "point 4 " + str(pt4) # # cv.Rectangle(img, (centerX + pt3[0], pt1[1] + pt3[1]),(centerX + pt4[0], pt1[1] + pt4[1]), cv.RGB(0, 255, 255)) # cv.Rectangle(img, pt3, pt4, cv.RGB(0, 0, 255)) if(minArea > 0): cv.Rectangle(img, (centerX + pt3[0], pt1[1] + pt3[1]), (centerX + pt4[0], pt1[1] + pt4[1]), cv.RGB(0, 255, 255)) pointX = centerX + pt3[0] + 10 pointY = pt1[1] + pt3[1] + 10 distanceX = pt4[0] - pt3[0] - 10 distanceY = pt4[1] - pt3[1] - 10 eyePart = cv.GetSubRect(img, (pointX, pointY, distanceX, distanceY))
def detect_face(img): haarFace = cv.Load('./haarcascade_frontalface_default.xml') #RGB_img = cv.fromarray(np.array(rgb[:,:,::-1])) allFaces = cv.HaarDetectObjects(cv.fromarray(img), haarFace, cv.CreateMemStorage(), scale_factor=1.1, min_neighbors=3, flags=0, min_size=(50, 50)) # Get confidences if (allFaces != []): count_no_face = 0 #print(allFaces) face_confid = [c for ((x, y, w, h), c) in allFaces] max_ind = np.argmax(face_confid) FINAL_FACE = allFaces[max_ind] return FINAL_FACE[0] else: return []
def getContours(im, approx_value=1): #Return contours approximated storage = cv.CreateMemStorage(0) contours = cv.FindContours(cv.CloneImage(im), storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) contourLow = cv.ApproxPoly(contours, storage, cv.CV_POLY_APPROX_DP, approx_value, approx_value) return contourLow
def findImageContour(img, frame): storage = cv.CreateMemStorage() cont = cv.FindContours(img, storage, cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_NONE, (0, 0)) max_center = [None, 0] for c in contour_iterator(cont): # Number of points must be more than or equal to 6 for cv.FitEllipse2 # Use to set minimum size of object to be tracked. if len(c) >= 60: # Copy the contour into an array of (x,y)s PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2) for (i, (x, y)) in enumerate(c): PointArray2D32f[0, i] = (x, y) # Fits ellipse to current contour. (center, size, angle) = cv.FitEllipse2(PointArray2D32f) # Only consider location of biggest contour -- adapt for multiple object tracking if size > max_center[1]: max_center[0] = center max_center[1] = size angle = angle if True: # Draw the current contour in gray gray = cv.CV_RGB(255, 255, 255) cv.DrawContours(img, c, gray, gray, 0, 1, 8, (0, 0)) if max_center[1] > 0: # Convert ellipse data from float to integer representation. center = (cv.Round(max_center[0][0]), cv.Round(max_center[0][1])) size = (cv.Round(max_center[1][0] * 0.5), cv.Round(max_center[1][1] * 0.5)) color = cv.CV_RGB(255, 0, 0) cv.Ellipse(frame, center, size, angle, 0, 360, color, 3, cv.CV_AA, 0)
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
def capture(): """ Using the intel training set to capture the face in the video. Most of them are frameworks in OpenCV. """ j = 0 g = os.walk("origin") for path, d, filelist in g: for filename in filelist: img = cv.LoadImage(os.path.join(path, filename)) image_size = cv.GetSize(img) greyscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(img, greyscale, cv.CV_BGR2GRAY) storage = cv.CreateMemStorage(0) cv.EqualizeHist(greyscale, greyscale) cascade = cv.Load('haarcascade_frontalface_alt2.xml') faces = cv.HaarDetectObjects(greyscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (50, 50)) for (x, y, w, h), n in faces: j += 1 cv.SetImageROI(img, (x, y, w, h)) cv.SaveImage("captured/face" + str(j) + ".png", img)
def handel_camera_image(img, hc): #resize it img2 = cv.CreateMat( cv.GetSize(img)[1] / 2, cv.GetSize(img)[0] / 2, cv.CV_8UC3) cv.Resize(img, img2) #convert to grayscale img_gray = cv.CreateImage(cv.GetSize(img2), 8, 1) cv.CvtColor(img2, img_gray, cv.CV_RGB2GRAY) #set the final image img_f = img_gray #detect faces from it objects = cv.HaarDetectObjects(img_f, hc, cv.CreateMemStorage()) number_of_faces = len(objects) if number_of_faces != 1: if debug: print "Error! Number of detected faces: " + str(number_of_faces) return None else: for (x, y, w, h), n in objects: #annotate the image cv.Rectangle(img_f, (x, y), (x + w, y + h), 255) if debug: print "FACE -> h: " + str(h) + ", w: " + str( w) + ", r(w/h): " + str(float(w) / float(h)) #resize to 64 to 64 img_r = resize_crop_img(img_f, x, y, w, h) return (img_f, img_r)
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 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 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 __init__(self, parent=None): QWidget.__init__(self) self.setMinimumSize(640, 480) self.setMaximumSize(self.minimumSize()) # register this callbacks to interact with the faces and the camera # image before the widget will view the frame self.image_callback = None self.face_callback = None # init view with correct size, depth, channels self.frame = cv.CreateImage((640, 480), cv.IPL_DEPTH_8U, 3) self.storage = cv.CreateMemStorage() self.capture = cv.CaptureFromCAM(0) self.face_cascade = cv.Load(CSC_PATH + "haarcascade_frontalface_alt.xml") self.fd_wait_frames = 1 self._fd_wait = self.fd_wait_frames # get first frame self._query_frame() # set refresh rate self.timer = QTimer(self) self.timer.timeout.connect(self._query_frame) self.timer.start(75)
def detect_face(self, image): 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, self.cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) return faces
def HandDetection(file): img=cv2.imread(file,0) #get size of input image w,h=img.shape[:2] #create grayscale version gray=np.zeros((h,w,3),np.uint8) #cv2.cvtColor(img, gray) gray=img # cv2.imshow('winname', gray) # cv2.waitKey(0) #create storage storage=cv.CreateMemStorage(0) #equalize histogram # cv.EqualizeHist(gray, gray) cascade_hand= cv2.CascadeClassifier('mycascade.xml') hands=cascade_hand.detectMultiScale(img, scaleFactor=1.3, minNeighbors=2, minSize=(24,24), flags=cv.CV_HAAR_DO_CANNY_PRUNING) for (x,y,w,h) in hands: cv2.rectangle(img, (x,y), (x+w,y+h), 255) cv2.imshow('winname', img) cv2.waitKey(0)
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 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 detectLeftEye(self, originalImage, cascade2, pt1, centerX, centerY): leftEyeArea = cv.GetSubRect(originalImage, (pt1[0], pt1[1], centerX - pt1[0], centerY - pt1[1])) leftEye = cv.HaarDetectObjects(leftEyeArea, cascade2, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # in case of multiple find the maximum box minArea = 0 pt3 = (0, 0) pt4 = (0, 0) if leftEye: for ((x, y, w, h), n) in leftEye: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints if(w * h > minArea): minArea = w * h pt3 = (x, y) pt4 = (x + w, y + h) if(minArea > 0): cv.Rectangle(originalImage, (pt1[0] + pt3[0], pt1[1] + pt3[1]), (pt1[0] + pt4[0], pt1[1] + pt4[1]), cv.RGB(255, 255, 0)) pointX = pt1[0] + pt3[0] + 10 pointY = pt1[1] + pt3[1] + 10 distanceX = pt4[0] - pt3[0] - 10 distanceY = pt4[1] - pt3[1] - 10 eyePart = cv.GetSubRect(originalImage, (pointX, pointY, distanceX, distanceY))
def detect_face(RGB_img): # Image Properties #print('Image Size: ({},{})'.format(cv.GetCaptureProperty(CAM_CAPT, cv.CV_CAP_PROP_FRAME_HEIGHT), cv.GetCaptureProperty(CAM_CAPT, cv.CV_CAP_PROP_FRAME_WIDTH))) #print('FPS: {}'.format(cv.GetCaptureProperty(CAM_CAPT, cv.CV_CAP_PROP_FPS))) RGB_img_mat = cv.fromarray(RGB_img) haarFace = cv.Load('haarcascade_frontalface_default.xml') #RGB_img = cv.fromarray(np.array(rgb[:,:,::-1])) allFaces = cv.HaarDetectObjects(RGB_img_mat, haarFace, cv.CreateMemStorage(), scale_factor=1.1, min_neighbors=10, flags=0, min_size=(50, 50)) # Get confidences if (allFaces != []): #print(allFaces) face_confid = [c for ((x, y, w, h), c) in allFaces] area = [w * h for ((x, y, w, h), c) in allFaces] #max_ind = np.argmax(face_confid) max_ind = np.argmax(area) FINAL_FACE = allFaces[max_ind] x0 = FINAL_FACE[0][0] y0 = FINAL_FACE[0][1] w = FINAL_FACE[0][2] h = FINAL_FACE[0][3] # Show detected face print('Face Detected!!') #cv.Rectangle(RGB_img_mat, (x0, y0), (x0+w, y0+h), cv.RGB(0,0,255), 2) # Detect eyes only in given face region print('Face: ' + str(FINAL_FACE)) cropped_img = RGB_img[y0:y0 + h, x0:x0 + w] #cv.Smooth(cropped_img, cropped_img, cv.CV_GAUSSIAN, 15, 15) #print(cv.GetSize(cropped_img)) #cv.ShowImage('crop', cropped_img) #cv.SaveImage('IMAGE.png', cropped_img) #allEyes = detect_eyes(cropped_img) #print('Eyes: '+str(allEyes)) #for eye in allEyes: # eye = eye[0] # eye=(eye[0]+x0, eye[1]+y0, eye[2], eye[3]) # cv.Rectangle(RGB_img, (eye[0], eye[1]), (eye[0]+eye[2], eye[1]+eye[3]), cv.RGB(255,0,0), 2) return np.asarray(cropped_img[:, :]) else: print('No Face!!') return RGB_img
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 update_mhi(img, dst, diff_threshold): global last global mhi global storage global mask global orient global segmask timestamp = time.clock() / CLOCKS_PER_SEC # get current time in seconds size = cv.GetSize(img) # get current frame size idx1 = last if not mhi or cv.GetSize(mhi) != size: for i in range(N): buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.Zero(buf[i]) mhi = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) cv.Zero(mhi) # clear MHI at the beginning orient = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) segmask = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) mask = cv.CreateImage(size,cv. IPL_DEPTH_8U, 1) cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY) # convert frame to grayscale idx2 = (last + 1) % N # index of (last - (N-1))th frame last = idx2 silh = buf[idx2] cv.AbsDiff(buf[idx1], buf[idx2], silh) # get difference between frames cv.Threshold(silh, silh, diff_threshold, 1, cv.CV_THRESH_BINARY) # and threshold it cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION) # update MHI cv.CvtScale(mhi, mask, 255./MHI_DURATION, (MHI_DURATION - timestamp)*255./MHI_DURATION) cv.Zero(dst) cv.Merge(mask, None, None, None, dst) cv.CalcMotionGradient(mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3) if not storage: storage = cv.CreateMemStorage(0) seq = cv.SegmentMotion(mhi, segmask, storage, timestamp, MAX_TIME_DELTA) for (area, value, comp_rect) in seq: if comp_rect[2] + comp_rect[3] > 100: # reject very small components color = cv.CV_RGB(255, 0,0) silh_roi = cv.GetSubRect(silh, comp_rect) mhi_roi = cv.GetSubRect(mhi, comp_rect) orient_roi = cv.GetSubRect(orient, comp_rect) mask_roi = cv.GetSubRect(mask, comp_rect) angle = 360 - cv.CalcGlobalOrientation(orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION) count = cv.Norm(silh_roi, None, cv.CV_L1, None) # calculate number of points within silhouette ROI if count < (comp_rect[2] * comp_rect[3] * 0.05): continue magnitude = 30. center = ((comp_rect[0] + comp_rect[2] / 2), (comp_rect[1] + comp_rect[3] / 2)) cv.Circle(dst, center, cv.Round(magnitude*1.2), color, 3, cv.CV_AA, 0) cv.Line(dst, center, (cv.Round(center[0] + magnitude * cos(angle * cv.CV_PI / 180)), cv.Round(center[1] - magnitude * sin(angle * cv.CV_PI / 180))), color, 3, cv.CV_AA, 0)
def find_Lines(im): out = cv.CreateImage(cv.GetSize(im), 8, 1) tmp = cv.CreateImage(cv.GetSize(im), 8, 3) storage = cv.CreateMemStorage(0) cv.Canny(im, out, 50, 200, 3) cv.CvtColor(out, tmp, cv.CV_GRAY2BGR) return cv.HoughLines2(out, storage, cv.CV_HOUGH_STANDARD, 1, pi / 180, 100, 0, 0)
def track(img, threshold=100): '''Accepts BGR image and optional object threshold between 0 and 255 (default = 100). Returns: (x,y) coordinates of centroid if found (-1,-1) if no centroid was found None if user hit ESC ''' cascade = cv.Load("haarcascade_frontalface_default.xml") 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) center = (-1, -1) faces = [] original_size_faces = [] #import ipdb; ipdb.set_trace() if (cascade): t = cv.GetTickCount() # HaarDetectObjects takes 0.02s faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t 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.Rectangle(img, (x,y), (x+w,y+h), 255) # get the xy corner co-ords, calc the center location x1 = pt1[0] x2 = pt2[0] y1 = pt1[1] y2 = pt2[1] centerx = x1 + ((x2 - x1) / 2) centery = y1 + ((y2 - y1) / 2) center = (centerx, centery) scaled = ((x1, y1, x2 - x1, y2 - y1), n) original_size_faces.append(scaled) # print scaled # cv.NamedWindow(WINDOW_NAME, 1) # cv.ShowImage(WINDOW_NAME, img) # if cv.WaitKey(5) == 27: # center = None return (center, original_size_faces)
def find_squares_from_binary( gray ): """ use contour search to find squares in binary image returns list of numpy arrays containing 4 points """ squares = [] storage = cv.CreateMemStorage(0) contours = cv.FindContours(gray, storage, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, (0,0)) storage = cv.CreateMemStorage(0) while contours: #approximate contour with accuracy proportional to the contour perimeter arclength = cv.ArcLength(contours) polygon = cv.ApproxPoly( contours, storage, cv.CV_POLY_APPROX_DP, arclength * 0.02, 0) if is_square(polygon): squares.append(polygon[0:4]) contours = contours.h_next() return squares
def initialize(self, frame): # Initialize # log_file_name = "tracker_output.log" # log_file = file( log_file_name, 'a' ) print str(type(frame)) print "resize to ::: " + str(cv.GetSize(frame)) + " " + str(type(frame)) (w, h) = cv.GetSize(frame) # gray = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1) size = (w, h) #cv.GetSize(frame)#(300 , 300) self.thumbnail = cv.CreateImage(size, cv.IPL_DEPTH_8U, 3) self.grey_average_image = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) self.grey_original_image = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) # cv.CvtColor(display_image, gray, cv.CV_RGB2GRAY) # prev_image = gray # Greyscale image, thresholded to create the motion mask: self.grey_image = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) # The RunningAvg() function requires a 32-bit or 64-bit image... self.running_average_image = cv.CreateImage(size, cv.IPL_DEPTH_32F, 3) # ...but the AbsDiff() function requires matching image depths: self.running_average_in_display_color_depth = cv.CloneImage(self.thumbnail) # RAM used by FindContours(): self.mem_storage = cv.CreateMemStorage(0) # The difference between the running average and the current frame: self.difference = cv.CloneImage(self.thumbnail) self.target_count = 1 self.last_target_count = 1 self.last_target_change_t = 0.0 self.k_or_guess = 1 self.codebook = [] self.last_frame_entity_list = [] self.frame_count = 0 # For toggling display: image_list = [ "camera", "difference", "threshold", "display", "faces" ] image_index = 3 # Index into image_list # Prep for text drawing: text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1, cv.CV_AA) text_coord = (5, 15) text_color = cv.CV_RGB(255, 255, 255) # Set this to the max number of targets to look for (passed to k-means): self.max_targets = 5
def lines2(): im = cv.LoadImage('roi_edges.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE) pi = math.pi x = 0 dst = cv.CreateImage(cv.GetSize(im), 8, 1) cv.Canny(im, dst, 200, 200) cv.Threshold(dst, dst, 100, 255, cv.CV_THRESH_BINARY) color_dst_standard = cv.CreateImage(cv.GetSize(im), 8, 3) cv.CvtColor(im, color_dst_standard, cv.CV_GRAY2BGR) #Create output image in RGB to put red lines lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_STANDARD, 1, pi / 100, 71, 0, 0) klsum = 0 klaver = 0 krsum = 0 kraver = 0 #global k #k=0 for (rho, theta) in lines[:100]: kl = [] kr = [] a = math.cos(theta) b = math.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))) k = ((y0 - 1000 * (a)) - (y0 + 1000 * (a))) / ((x0 - 1000 * (-b)) - (x0 + 1000 * (-b))) if abs(k) < 0.4: pass elif k > 0: kr.append(k) len_kr = len(kr) for i in kr: krsum = krsum + i kraver = krsum / len_kr cv.Line(color_dst_standard, pt1, pt2, cv.CV_RGB(255, 0, 0), 2, 4) elif k < 0: kr.append(k) kl.append(k) len_kl = len(kl) for i in kl: klsum = klsum + i klaver = klsum / len_kl cv.Line(color_dst_standard, pt1, pt2, cv.CV_RGB(255, 0, 0), 2, 4) #print k # cv.Line(color_dst_standard, pt1, pt2, cv.CV_RGB(255, 0, 0), 2, 4) cv.SaveImage('lane.jpg', color_dst_standard) print '左车道平均斜率:', klaver, ' 右车道平均斜率:', kraver cv.ShowImage("Hough Standard", color_dst_standard) cv.WaitKey(0)
def find_rectangles(self,input_img): """ Find contours in the input image. input_img: Is a binary image """ contours_img=cv.CreateMat(input_img.height, input_img.width, cv.CV_8UC1) # Image to draw the contours copied_img=cv.CreateMat(input_img.height, input_img.width, input_img.type) # Image to draw the contours cv.Copy(input_img, copied_img) contours = cv.FindContours(copied_img,cv.CreateMemStorage(),cv.CV_RETR_TREE,cv.CV_CHAIN_APPROX_SIMPLE) cv.DrawContours(contours_img,contours,255,0,10) return contours_img
def OnPaint(self, evt): if not self.timer.IsRunning() : dc = wx.BufferedDC(wx.ClientDC(self), wx.NullBitmap, wx.BUFFER_VIRTUAL_AREA) dc.SetBackground(wx.Brush(wx.Colour(0, 0, 0))) return # Capture de l'image frame = cv.QueryFrame(CAMERA) cv.CvtColor(frame, frame, cv.CV_BGR2RGB) Img = wx.EmptyImage(frame.width, frame.height) Img.SetData(frame.tostring()) self.bmp = wx.BitmapFromImage(Img) width, height = frame.width, frame.height # Détection des visages min_size = (20, 20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 gray = cv.CreateImage((frame.width, frame.height), 8, 1) small_img = cv.CreateImage((cv.Round(frame.width / image_scale), cv.Round (frame.height / image_scale)), 8, 1) cv.CvtColor(frame, gray, cv.CV_BGR2GRAY) cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) listeVisages = cv.HaarDetectObjects(small_img, CASCADE, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # Affichage de l'image x, y = (0, 0) try: dc = wx.BufferedDC(wx.ClientDC(self), wx.NullBitmap, wx.BUFFER_VIRTUAL_AREA) try : dc.SetBackground(wx.Brush(wx.Colour(0, 0, 0))) except : pass dc.Clear() dc.DrawBitmap(self.bmp, x, y) # Dessin des rectangles des visages if listeVisages : for ((x, y, w, h), n) in listeVisages : dc.SetBrush(wx.TRANSPARENT_BRUSH) dc.SetPen(wx.Pen(wx.Colour(255, 0, 0), 2)) dc.DrawRectangle(x* image_scale, y* image_scale, w* image_scale, h* image_scale) self.listeVisages = listeVisages del dc del Img except TypeError: pass except wx.PyDeadObjectError: pass