def findContours(image, getPolygon): storage = cv.cvCreateMemStorage(0) polyContourArray = [] polyStorage = cv.cvCreateMemStorage(0) nb_contours, contours = cv.cvFindContours(image, storage, cv.sizeof_CvContour, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, cv.cvPoint(0, 0)) if contours == None: return None contoursList = list(contours.hrange()) if not getPolygon: ret = contoursList else: for contour in contoursList: per = cvContourPerimeter(contour) polyContourArray.append( cv.cvApproxPoly(contour, cv.sizeof_CvContour, storage, cv.CV_POLY_APPROX_DP, per / PER_TOLERANCE, 0)) ret = polyContourArray return ret
def findContours(image,getPolygon): storage = cv.cvCreateMemStorage (0) polyContourArray=[] polyStorage=cv.cvCreateMemStorage (0) nb_contours, contours = cv.cvFindContours (image, storage, cv.sizeof_CvContour, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, cv.cvPoint (0,0)) if contours==None: return None contoursList=list(contours.hrange()) if not getPolygon: ret=contoursList else: for contour in contoursList: per=cvContourPerimeter(contour) polyContourArray.append(cv.cvApproxPoly (contour, cv.sizeof_CvContour, storage, cv.CV_POLY_APPROX_DP, per/PER_TOLERANCE, 0)) ret=polyContourArray return ret
def detectObject(image): grayscale = cv.cvCreateImage(size, 8, 1) cv.cvFlip(image, None, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) cv.cvEqualizeHist(grayscale, grayscale) cascade = cv.cvLoadHaarClassifierCascade(haar_file, cv.cvSize(1, 1)) objects = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100, 100)) # Draw dots where hands are if objects: for i in objects: #cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)), # cv.cvPoint(int(i.x+i.width), int(i.y+i.height)), # cv.CV_RGB(0,255,0), 3, 8, 0) center = cv.cvPoint(int(i.x + i.width / 2), int(i.y + i.height / 2)) cv.cvCircle(image, center, 10, cv.CV_RGB(0, 0, 0), 5, 8, 0) # Left side check if center.x > box_forward_left[ 0].x and center.x < box_backwards_left[ 1].x and center.y > box_forward_left[ 0].y and center.y < box_backwards_left[1].y: set_speed('left', center) # Right side check if center.x > box_forward_right[ 0].x and center.x < box_backwards_right[ 1].x and center.y > box_forward_right[ 0].y and center.y < box_backwards_right[1].y: set_speed('right', center)
def __findcurve(self, img): storage = cv.cvCreateMemStorage(0) nb_contours, cont = cv.cvFindContours(img, storage, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint(0, 0)) cidx = int(random.random() * len(color)) if (self.drawcontour): cv.cvDrawContours(self.drawimg, cont, _white, _white, 1, 1, cv.CV_AA, cv.cvPoint(0, 0)) idx = 0 for c in cont.hrange(): PointArray = cv.cvCreateMat(1, c.total, cv.CV_32SC2) PointArray2D32f = cv.cvCreateMat(1, c.total, cv.CV_32FC2) cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)) fpoints = [] for i in range(c.total): kp = myPoint() kp.x = cv.cvGet2D(PointArray, 0, i)[0] kp.y = cv.cvGet2D(PointArray, 0, i)[1] kp.index = idx idx += 1 fpoints.append(kp) self.allcurve.append(fpoints) self.curvelength = idx
def detect(image, cascade_file='haarcascade_data/haarcascade_frontalface_alt.xml'): image_size = cv.cvGetSize(image) # create grayscale version grayscale = cv.cvCreateImage(image_size, 8, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) # create storage storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) # equalize histogram cv.cvEqualizeHist(grayscale, grayscale) # detect objects cascade = cv.cvLoadHaarClassifierCascade(cascade_file, cv.cvSize(1,1)) faces = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(50, 50)) positions = [] if faces: for i in faces: positions.append({'x': i.x, 'y': i.y, 'width': i.width, 'height': i.height,}) cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)), cv.cvPoint(int(i.x + i.width), int(i.y + i.height)), cv.CV_RGB(0, 255, 0), 3, 8, 0) return positions
def detectObject(image): grayscale = cv.cvCreateImage(size, 8, 1) cv.cvFlip(image, None, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) cv.cvEqualizeHist(grayscale, grayscale) cascade = cv.cvLoadHaarClassifierCascade(haar_file, cv.cvSize(1,1)) objects = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100,100)) # Draw dots where hands are if objects: for i in objects: #cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)), # cv.cvPoint(int(i.x+i.width), int(i.y+i.height)), # cv.CV_RGB(0,255,0), 3, 8, 0) center = cv.cvPoint(int(i.x+i.width/2), int(i.y+i.height/2)) cv.cvCircle(image, center, 10, cv.CV_RGB(0,0,0), 5,8, 0) # Left side check if center.x > box_forward_left[0].x and center.x < box_backwards_left[1].x and center.y > box_forward_left[0].y and center.y < box_backwards_left[1].y: set_speed('left', center) # Right side check if center.x > box_forward_right[0].x and center.x < box_backwards_right[1].x and center.y > box_forward_right[0].y and center.y < box_backwards_right[1].y: set_speed('right', center)
def __findContour(self, filename): #find the contour of images, and save all points in self.vKeyPoints self.img = highgui.cvLoadImage (filename) self.grayimg = cv.cvCreateImage(cv.cvSize(self.img.width, self.img.height), 8,1) self.drawimg = cv.cvCreateImage(cv.cvSize(self.img.width, self.img.height), 8,3) cv.cvCvtColor (self.img, self.grayimg, cv.CV_BGR2GRAY) cv.cvSmooth(self.grayimg, self.grayimg, cv.CV_BLUR, 9) cv.cvSmooth(self.grayimg, self.grayimg, cv.CV_BLUR, 9) cv.cvSmooth(self.grayimg, self.grayimg, cv.CV_BLUR, 9) cv.cvThreshold( self.grayimg, self.grayimg, self.threshold, self.threshold +100, cv.CV_THRESH_BINARY ) cv.cvZero(self.drawimg) storage = cv.cvCreateMemStorage(0) nb_contours, cont = cv.cvFindContours (self.grayimg, storage, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint (0,0)) cv.cvDrawContours (self.drawimg, cont, cv.cvScalar(255,255,255,0), cv.cvScalar(255,255,255,0), 1, 1, cv.CV_AA, cv.cvPoint (0, 0)) self.allcurve = [] idx = 0 for c in cont.hrange(): PointArray = cv.cvCreateMat(1, c.total , cv.CV_32SC2) PointArray2D32f= cv.cvCreateMat( 1, c.total , cv.CV_32FC2) cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)) fpoints = [] for i in range(c.total): kp = myPoint() kp.x = cv.cvGet2D(PointArray,0, i)[0] kp.y = cv.cvGet2D(PointArray,0, i)[1] kp.index = idx idx += 1 fpoints.append(kp) self.allcurve.append(fpoints) self.curvelength = idx
def __findcurve(self, img): storage = cv.cvCreateMemStorage(0) nb_contours, cont = cv.cvFindContours (img, storage, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint (0,0)) cidx = int(random.random() * len(color)) if (self.drawcontour): cv.cvDrawContours (self.drawimg, cont, _white, _white, 1, 1, cv.CV_AA, cv.cvPoint (0, 0)) idx = 0 for c in cont.hrange(): PointArray = cv.cvCreateMat(1, c.total, cv.CV_32SC2) PointArray2D32f= cv.cvCreateMat( 1, c.total , cv.CV_32FC2) cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)) fpoints = [] for i in range(c.total): kp = myPoint() kp.x = cv.cvGet2D(PointArray,0, i)[0] kp.y = cv.cvGet2D(PointArray,0, i)[1] kp.index = idx idx += 1 fpoints.append(kp) self.allcurve.append(fpoints) self.curvelength = idx
def init(self): #Load the cascade classifier data self.cascade=cv.cvLoadHaarClassifierCascade(self.file_path(self.cascade_file),cv.cvSize(self.haar_face_size[0],self.haar_face_size[1])) #Allocate/init storage self.storage=cv.cvCreateMemStorage(0) cv.cvClearMemStorage(self.storage)
def face_detect(file, closeafter=True): """Converts an image to grayscale and prints the locations of any faces found""" if hasattr(file, 'read'): _, filename = tempfile.mkstemp() tmphandle = open(filename, 'w') shutil.copyfileobj(file, tmphandle) tmphandle.close() if closeafter: file.close() deleteafter = True else: filename = file deleteafter = False image = cvLoadImage(filename) grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1) cvCvtColor(image, grayscale, CV_BGR2GRAY) storage = cvCreateMemStorage(0) cvClearMemStorage(storage) cvEqualizeHist(grayscale, grayscale) cascade = cvLoadHaarClassifierCascade( '/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml', cvSize(1,1)) faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50)) if deleteafter: os.unlink(filename) return (image.width, image.height), faces
def __init__(self, *args): apply(QWidget.__init__,(self, ) + args) self.cascade_name = 'haarcascades/haarcascade_frontalface_alt.xml' self.cascade = cv.cvLoadHaarClassifierCascade(self.cascade_name, cv.cvSize(20,20)) self.storage = cv.cvCreateMemStorage(0) self.cap = highgui.cvCreateCameraCapture(0) self.q_buffer = QImage() self.q_buffer.create(420,300,8) self.timer = self.startTimer(1)
def __init__(self, *args): apply(QWidget.__init__, (self, ) + args) self.cascade_name = 'haarcascades/haarcascade_frontalface_alt.xml' self.cascade = cv.cvLoadHaarClassifierCascade(self.cascade_name, cv.cvSize(20, 20)) self.storage = cv.cvCreateMemStorage(0) self.cap = highgui.cvCreateCameraCapture(0) self.q_buffer = QImage() self.q_buffer.create(420, 300, 8) self.timer = self.startTimer(1)
def _detect(image): """ Detects faces on `image` Parameters: @image: image file path Returns: [((x1, y1), (x2, y2)), ...] List of coordenates for top-left and bottom-right corner """ # the OpenCV API says this function is obsolete, but we can't # cast the output of cvLoad to a HaarClassifierCascade, so use # this anyways the size parameter is ignored frame = cvLoadImage(image) if not frame: return [] img = cvCreateImage(cvSize(frame.width, frame.height), IPL_DEPTH_8U, frame.nChannels) cvCopy(frame, img) # allocate temporary images gray = cvCreateImage((img.width, img.height), COPY_DEPTH, COPY_CHANNELS) width, height = (cvRound(img.width / IMAGE_SCALE), cvRound(img.height / IMAGE_SCALE)) small_img = cvCreateImage((width, height), COPY_DEPTH, COPY_CHANNELS) storage = cvCreateMemStorage(0) try: # convert color input image to grayscale cvCvtColor(img, gray, CV_BGR2GRAY) # scale input image for faster processing cvResize(gray, small_img, CV_INTER_LINEAR) cvEqualizeHist(small_img, small_img) coords = [] for haar_file in CASCADES: cascade = cvLoadHaarClassifierCascade(haar_file, cvSize(1, 1)) if cascade: faces = cvHaarDetectObjects(small_img, cascade, storage, HAAR_SCALE, MIN_NEIGHBORS, HAAR_FLAGS, MIN_SIZE) or [] for face_rect in faces: # the input to cvHaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints x, y = face_rect.x, face_rect.y pt1 = (int(x * IMAGE_SCALE), int(y * IMAGE_SCALE)) pt2 = (int((x + face_rect.width) * IMAGE_SCALE), int((y + face_rect.height) * IMAGE_SCALE)) coords.append((pt1, pt2)) finally: cvClearMemStorage(storage) return coords
def get_contours(self,image,threshold): storage=cv.cvCreateMemStorage(0) image=self.clone_image(image) num_contours,contours=cv.cvFindContours(image,storage,cv.sizeof_CvContour,cv.CV_RETR_LIST,cv.CV_CHAIN_APPROX_NONE,cv.cvPoint(0,0)) contour_points=[[]]*num_contours for contour_index,contour in enumerate(contours.hrange()): for point in contour: contour_points[contour_index].append((point.x,point.y)) cv.cvReleaseImage(image) cv.cvReleaseMemStorage(storage) return contour_points
def detect_faces_on(path): faces = [] image = cvLoadImage(path) # convert to grayscale for faster results grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1) cvCvtColor(image, grayscale, CV_BGR2GRAY) # smooth picture for better results cvSmooth(grayscale, grayscale, CV_GAUSSIAN, 3, 3) storage = cvCreateMemStorage(0) cvClearMemStorage(storage) cvEqualizeHist(grayscale, grayscale) cascade_files = [ # ('/usr/share/opencv/haarcascades/haarcascade_eye_tree_eyeglasses.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_lowerbody.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_mcs_mouth.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_profileface.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_eye.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_mcs_eyepair_big.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_mcs_nose.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_righteye_2splits.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_fullbody.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_mcs_eyepair_small.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_mcs_righteye.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_upperbody.xml', (50, 50)), ('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_lefteye_2splits.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_mcs_lefteye.xml', (50, 50)), # ('/usr/share/opencv/haarcascades/haarcascade_mcs_upperbody.xml', (50, 50)), # ('parojos_22_5.1.xml', (22, 5)), # ('Mouth.xml', (22, 15)), ] for cascade_file, cascade_sizes in cascade_files: cascade = cvLoadHaarClassifierCascade(os.path.join(cascade_file), cvSize(1, 1)) faces += cvHaarDetectObjects(grayscale, cascade, storage, HAAR_SCALE, HAAR_NEIGHBORS, CV_HAAR_DO_CANNY_PRUNING, cvSize(*cascade_sizes)) return [{'x': f.x, 'y': f.y, 'w': f.width, 'h': f.height} for f in faces]
def detect_faces(image): """Converts an image to grayscale and prints the locations of any faces found""" grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1) cvCvtColor(image, grayscale, CV_BGR2GRAY) storage = cvCreateMemStorage(0) cvClearMemStorage(storage) cvEqualizeHist(grayscale, grayscale) # The default parameters (scale_factor=1.1, min_neighbors=3, # flags=0) are tuned for accurate yet slow face detection. For # faster face detection on real video images the better settings are # (scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING). # --- http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_Experimental.htm#decl_cvHaarDetectObjects # The size box is of the *minimum* detectable object size. Smaller box = more processing time. - http://cell.fixstars.com/opencv/index.php/Facedetect minsize = (int(MINFACEWIDTH_PERCENT * image.width + 0.5), int(MINFACEHEIGHT_PERCENT * image.height)) print >> sys.stderr, "Min size of face: %s" % ` minsize ` faces = [] for cascadefile in [ '/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml' ]: # for cascadefile in ['/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml', '/usr/share/opencv/haarcascades/haarcascade_profileface.xml']: cascade = cvLoadHaarClassifierCascade(cascadefile, cvSize(1, 1)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 3, 0, cvSize(MINFACEWIDTH,MINFACEHEIGHT)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 3, 0, cvSize(MINFACEWIDTH,MINFACEHEIGHT)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(*minsize)) faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 4, CV_HAAR_DO_CANNY_PRUNING, cvSize(*minsize)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, scale_factor=1.1, min_neighbors=3, flags=0, cvSize(50,50)) # print dir(faces) bboxes = [] if faces: for f in faces: print >> sys.stderr, "\tFace at [(%d,%d) -> (%d,%d)]" % ( f.x, f.y, f.x + f.width, f.y + f.height) bboxes = [Face(f.x, f.y, f.x + f.width, f.y + f.height) for f in faces] return bboxes
def detectObjects(image): """Converts an image to grayscale and prints the locations of any faces found""" size = cv.cvSize(image.width, image.height) grayscale = cv.cvCreateImage(size, 8, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) cv.cvEqualizeHist(grayscale, grayscale) cascade = cv.cvLoadHaarClassifierCascade(PATH, cv.cvSize(1,1)) faces = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(30,30)) if faces: for f in faces: print("[(%d,%d) -> (%d,%d)]" % (f.x, f.y, f.x+f.width, f.y+f.height))
def detect(image): image_size = opencv.cvGetSize(image) # create grayscale version grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(100, 100)) # eyes = opencv.cvHaarDetectObjects(grayscale, eye_cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(60,60)) draw_bounding_boxes(faces, image, 127,255,0, 3)
def detect(image): image_size = cv.cvGetSize(image) # create grayscale version grayscale = cv.cvCreateImage(image_size, 8, 1) cv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) # equalize histogram cv.cvEqualizeHist(grayscale, grayscale) # detect objects cascade = cv.cvLoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.cvSize(1,1)) faces = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100, 100)) if faces: for i in faces: r = image[int(i.y):int(i.y+i.height),int(i.x):int(i.x+i.width)] cv.cvSmooth(r,r,cv.CV_BLUR,51,51)
def detect(self, pil_image, cascade_name, recogn_w = 50, recogn_h = 50): # Get cascade: cascade = self.get_cascade(cascade_name) image = opencv.PIL2Ipl(pil_image) image_size = opencv.cvGetSize(image) grayscale = image if pil_image.mode == "RGB": # create grayscale version grayscale = opencv.cvCreateImage(image_size, 8, 1) # Change to RGB2Gray - I dont think itll affect the conversion opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects return opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(recogn_w, recogn_h))
def detect_faces(image): """Converts an image to grayscale and prints the locations of any faces found""" grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1) cvCvtColor(image, grayscale, CV_BGR2GRAY) storage = cvCreateMemStorage(0) cvClearMemStorage(storage) cvEqualizeHist(grayscale, grayscale) # The default parameters (scale_factor=1.1, min_neighbors=3, # flags=0) are tuned for accurate yet slow face detection. For # faster face detection on real video images the better settings are # (scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING). # --- http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_Experimental.htm#decl_cvHaarDetectObjects # The size box is of the *minimum* detectable object size. Smaller box = more processing time. - http://cell.fixstars.com/opencv/index.php/Facedetect minsize = (int(MINFACEWIDTH_PERCENT*image.width+0.5),int(MINFACEHEIGHT_PERCENT*image.height)) print >> sys.stderr, "Min size of face: %s" % `minsize` faces = [] for cascadefile in ['/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml']: # for cascadefile in ['/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml', '/usr/share/opencv/haarcascades/haarcascade_profileface.xml']: cascade = cvLoadHaarClassifierCascade(cascadefile, cvSize(1,1)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 3, 0, cvSize(MINFACEWIDTH,MINFACEHEIGHT)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 3, 0, cvSize(MINFACEWIDTH,MINFACEHEIGHT)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(*minsize)) faces += cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 4, CV_HAAR_DO_CANNY_PRUNING, cvSize(*minsize)) # faces += cvHaarDetectObjects(grayscale, cascade, storage, scale_factor=1.1, min_neighbors=3, flags=0, cvSize(50,50)) # print dir(faces) bboxes = [] if faces: for f in faces: print >> sys.stderr, "\tFace at [(%d,%d) -> (%d,%d)]" % (f.x, f.y, f.x+f.width, f.y+f.height) bboxes = [Face(f.x, f.y, f.x+f.width, f.y+f.height) for f in faces] return bboxes
def detect(self, pil_image, cascade_name, recogn_w=50, recogn_h=50): # Get cascade: cascade = self.get_cascade(cascade_name) image = opencv.PIL2Ipl(pil_image) image_size = opencv.cvGetSize(image) grayscale = image if pil_image.mode == "RGB": # create grayscale version grayscale = opencv.cvCreateImage(image_size, 8, 1) # Change to RGB2Gray - I dont think itll affect the conversion opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects return opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(recogn_w, recogn_h))
IPL_DEPTH_8U, cvCreateMemStorage CASCADES_DIR = dirname(abspath(__file__)) CASCADES_NAMES = ['haarcascade_frontalface_alt_tree.xml'] CASCADES = [ join(CASCADES_DIR, name) for name in CASCADES_NAMES ] MIN_SIZE = cvSize(100, 100) IMAGE_SCALE = 1.3 MIN_NEIGHBORS = 3 HAAR_SCALE = 1.3 HAAR_FLAGS = CV_HAAR_DO_CANNY_PRUNING DEFAULT_ROTATE_ANGLE = 45 COPY_DEPTH = 8 COPY_CHANNELS = 1 STORAGE = cvCreateMemStorage(0) def _detect(image): """ Detects faces on `image` Parameters: @image: image file path Returns: [((x1, y1), (x2, y2)), ...] List of coordenates for top-left and bottom-right corner """ # the OpenCV API says this function is obsolete, but we can't # cast the output of cvLoad to a HaarClassifierCascade, so use # this anyways the size parameter is ignored capture = cvCreateFileCapture(image)
left = Motor(brick, PORT_C) capture = hg.cvCreateCameraCapture(1) # padroes reconhecidos # known patterns padroes = ("triangulo.png", "circulo.png", "H.png", "F.png") color = (cv.CV_RGB(255,0,0),cv.CV_RGB(0,255,0),cv.CV_RGB(0,0,255),cv.CV_RGB(255,255,0)) limiar = (0.01, 0.001, 0.21, 0.18) target = [] target_mod = [] target_c = [] # Armazenamento temporario # Temporary storage storage1 = cv.cvCreateMemStorage() storage2 = cv.cvCreateMemStorage() # Carrega, binariza e procura o contorno dos padroes # Load, binarize and find contours for i in range(0,len(padroes)): target.append(hg.cvLoadImage(padroes[i], hg.CV_LOAD_IMAGE_GRAYSCALE)) target_mod.append(cv.cvCreateImage((target[i].width,target[i].height), cv.IPL_DEPTH_8U, 1)) cv.cvThreshold(target[i],target_mod[i],200,254,cv.CV_THRESH_BINARY) target_c.append(cv.cvFindContours(target_mod[i], storage1)[1]) target_c[i] = cv.cvApproxPoly(target_c[i],cv.sizeof_CvContour,storage1,cv.CV_POLY_APPROX_DP,1) frame_mod = cv.cvCreateImage((hg.cvGetCaptureProperty(capture,hg.CV_CAP_PROP_FRAME_WIDTH), hg.cvGetCaptureProperty(capture,hg.CV_CAP_PROP_FRAME_HEIGHT)), cv.IPL_DEPTH_8U, 1) hg.cvNamedWindow("Resultado") hg.cvNamedWindow("Binarizado")
def timerEvent(self, ev): # Fetch a frame from the video camera frame = highgui.cvQueryFrame(self.cap) img_orig = cv.cvCreateImage(cv.cvSize(frame.width, frame.height), cv.IPL_DEPTH_8U, frame.nChannels) if (frame.origin == cv.IPL_ORIGIN_TL): cv.cvCopy(frame, img_orig) else: cv.cvFlip(frame, img_orig, 0) # Create a grey frame to clarify data img_grey = cv.cvCreateImage(cv.cvSize(img_orig.width, img_orig.height), 8, 1) cv.cvCvtColor(img_orig, img_grey, cv.CV_BGR2GRAY) # Detect objects within the frame self.faces_storage = cv.cvCreateMemStorage(0) faces = self.detect_faces(img_grey) self.circles_storage = cv.cvCreateMemStorage(0) circles = self.detect_circles(img_grey) self.squares_storage = cv.cvCreateMemStorage(0) squares = self.detect_squares(img_grey, img_orig) self.lines_storage = cv.cvCreateMemStorage(0) lines = self.detect_lines(img_grey, img_orig) # Draw faces if faces: for face in faces: pt1, pt2 = self.face_points(face) cv.cvRectangle(img_orig, pt1, pt2, cv.CV_RGB(255, 0, 0), 3, 8, 0) # Draw lines if lines: for line in lines: cv.cvLine(img_orig, line[0], line[1], cv.CV_RGB(255, 255, 0), 3, 8) # Draw circles if circles: for circle in circles: cv.cvCircle( img_orig, cv.cvPoint(cv.cvRound(circle[0]), cv.cvRound(circle[1])), cv.cvRound(circle[2]), cv.CV_RGB(0, 0, 255), 3, 8, 0) # Draw squares if squares: i = 0 while i < squares.total: pt = [] # read 4 vertices pt.append(squares[i]) pt.append(squares[i + 1]) pt.append(squares[i + 2]) pt.append(squares[i + 3]) ## draw the square as a closed polyline cv.cvPolyLine(img_orig, [pt], 1, cv.CV_RGB(0, 255, 0), 3, cv.CV_AA, 0) i += 4 # Resize the image to display properly within the window # CV_INTER_NN - nearest-neigbor interpolation, # CV_INTER_LINEAR - bilinear interpolation (used by default) # CV_INTER_AREA - resampling using pixel area relation. (preferred for image decimation) # CV_INTER_CUBIC - bicubic interpolation. img_display = cv.cvCreateImage(cv.cvSize(self.width(), self.height()), 8, 3) cv.cvResize(img_orig, img_display, cv.CV_INTER_NN) img_pil = adaptors.Ipl2PIL(img_display) s = StringIO() img_pil.save(s, "PNG") s.seek(0) q_img = QImage() q_img.loadFromData(s.read()) bitBlt(self, 0, 0, q_img)
def process_image(slider_pos): """ Define trackbar callback functon. This function find contours, draw it and approximate it by ellipses. """ stor = cv.cvCreateMemStorage(0) # Threshold the source image. This needful for cv.cvFindContours(). cv.cvThreshold(image03, image02, slider_pos, 255, cv.CV_THRESH_BINARY) # Find all contours. nb_contours, cont = cv.cvFindContours(image02, stor, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint(0, 0)) # Clear images. IPL use. cv.cvZero(image02) cv.cvZero(image04) # This cycle draw all contours and approximate it by ellipses. for c in cont.hrange(): count = c.total # This is number point in contour # Number point must be more than or equal to 6 (for cv.cvFitEllipse_32f). if (count < 6): continue # Alloc memory for contour point set. PointArray = cv.cvCreateMat(1, count, cv.CV_32SC2) PointArray2D32f = cv.cvCreateMat(1, count, cv.CV_32FC2) # Get contour point set. cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)) # Convert CvPoint set to CvBox2D32f set. cv.cvConvert(PointArray, PointArray2D32f) box = cv.CvBox2D() # Fits ellipse to current contour. box = cv.cvFitEllipse2(PointArray2D32f) # Draw current contour. cv.cvDrawContours(image04, c, cv.CV_RGB(255, 255, 255), cv.CV_RGB(255, 255, 255), 0, 1, 8, cv.cvPoint(0, 0)) # Convert ellipse data from float to integer representation. center = cv.CvPoint() size = cv.CvSize() center.x = cv.cvRound(box.center.x) center.y = cv.cvRound(box.center.y) size.width = cv.cvRound(box.size.width * 0.5) size.height = cv.cvRound(box.size.height * 0.5) box.angle = -box.angle # Draw ellipse. cv.cvEllipse(image04, center, size, box.angle, 0, 360, cv.CV_RGB(0, 0, 255), 1, cv.CV_AA, 0) # Show image. HighGUI use. highgui.cvShowImage("Result", image04)
CV_HAAR_DO_CANNY_PRUNING, CV_INTER_LINEAR, CV_BGR2GRAY, \ IPL_DEPTH_8U, cvCreateMemStorage CASCADES_DIR = dirname(abspath(__file__)) CASCADES_NAMES = ['haarcascade_frontalface_alt_tree.xml'] CASCADES = [join(CASCADES_DIR, name) for name in CASCADES_NAMES] MIN_SIZE = cvSize(100, 100) IMAGE_SCALE = 1.3 MIN_NEIGHBORS = 3 HAAR_SCALE = 1.3 HAAR_FLAGS = CV_HAAR_DO_CANNY_PRUNING DEFAULT_ROTATE_ANGLE = 45 COPY_DEPTH = 8 COPY_CHANNELS = 1 STORAGE = cvCreateMemStorage(0) def _detect(image): """ Detects faces on `image` Parameters: @image: image file path Returns: [((x1, y1), (x2, y2)), ...] List of coordenates for top-left and bottom-right corner """ # the OpenCV API says this function is obsolete, but we can't # cast the output of cvLoad to a HaarClassifierCascade, so use # this anyways the size parameter is ignored capture = cvCreateFileCapture(image)
0, 0, 360, _black, -1, 8, 0) cv.cvEllipse (image, cv.cvPoint (dx + 27, dy + 100), cv.cvSize (20, 35), 0, 0, 360, _white, -1, 8, 0) cv.cvEllipse (image, cv.cvPoint (dx + 273, dy + 100), cv.cvSize (20, 35), 0, 0, 360, _white, -1, 8, 0) # create window and display the original picture in it highgui.cvNamedWindow ("image", 1) highgui.cvShowImage ("image", image) # create the storage area storage = cv.cvCreateMemStorage (0) # find the contours nb_contours, contours = cv.cvFindContours (image, storage, cv.sizeof_CvContour, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, cv.cvPoint (0,0)) # comment this out if you do not want approximation contours = cv.cvApproxPoly (contours, cv.sizeof_CvContour, storage, cv.CV_POLY_APPROX_DP, 3, 1) # create the window for the contours
from opencv.cv import cvCreateMemStorage, cvMinAreaRect2, CvBox2D, cvContourArea, cvSize2D32f, cvContourPerimeter MINCONTOUR_AREA = 1000 BOXFILTER_TOLERANCE = 0.6 #MINCONTOUR_PERIMETER=40 MINCONTOUR_PERIMETER = 80 CONTOUR_RECTANGULAR_MIN_RATIO = 1.2 CONTOUR_RECTANGULAR_MAX_RATIO = 3.0 mem = cvCreateMemStorage(0) def rectangularAspectFilter(contour): ret = False try: box = cvMinAreaRect2(contour, mem) if box.size.width > CONTOUR_RECTANGULAR_MIN_RATIO* box.size.height and\ box.size.width < CONTOUR_RECTANGULAR_MAX_RATIO* box.size.height\ or box.size.height > CONTOUR_RECTANGULAR_MIN_RATIO* box.size.width and\ box.size.height < CONTOUR_RECTANGULAR_MAX_RATIO* box.size.width: ret = True else: #print("No paso el rectangular Fitlter") ret = False except: pass return ret
from opencv.cv import cvCreateMemStorage,cvMinAreaRect2,CvBox2D, cvContourArea, cvSize2D32f, cvContourPerimeter MINCONTOUR_AREA=1000 BOXFILTER_TOLERANCE=0.6 #MINCONTOUR_PERIMETER=40 MINCONTOUR_PERIMETER=80 CONTOUR_RECTANGULAR_MIN_RATIO=1.2 CONTOUR_RECTANGULAR_MAX_RATIO=3.0 mem=cvCreateMemStorage(0) def rectangularAspectFilter(contour): ret=False try: box=cvMinAreaRect2(contour,mem) if box.size.width > CONTOUR_RECTANGULAR_MIN_RATIO* box.size.height and\ box.size.width < CONTOUR_RECTANGULAR_MAX_RATIO* box.size.height\ or box.size.height > CONTOUR_RECTANGULAR_MIN_RATIO* box.size.width and\ box.size.height < CONTOUR_RECTANGULAR_MAX_RATIO* box.size.width: ret=True else: #print("No paso el rectangular Fitlter") ret=False except: pass return ret
def process_image( slider_pos ): """ Define trackbar callback functon. This function find contours, draw it and approximate it by ellipses. """ stor = cv.cvCreateMemStorage(0); # Threshold the source image. This needful for cv.cvFindContours(). cv.cvThreshold( image03, image02, slider_pos, 255, cv.CV_THRESH_BINARY ); # Find all contours. nb_contours, cont = cv.cvFindContours (image02, stor, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint (0,0)) # Clear images. IPL use. cv.cvZero(image02); cv.cvZero(image04); # This cycle draw all contours and approximate it by ellipses. for c in cont.hrange(): count = c.total; # This is number point in contour # Number point must be more than or equal to 6 (for cv.cvFitEllipse_32f). if( count < 6 ): continue; # Alloc memory for contour point set. PointArray = cv.cvCreateMat(1, count, cv.CV_32SC2) PointArray2D32f= cv.cvCreateMat( 1, count, cv.CV_32FC2) # Get contour point set. cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)); # Convert CvPoint set to CvBox2D32f set. cv.cvConvert( PointArray, PointArray2D32f ) box = cv.CvBox2D() # Fits ellipse to current contour. box = cv.cvFitEllipse2(PointArray2D32f); # Draw current contour. cv.cvDrawContours(image04, c, cv.CV_RGB(255,255,255), cv.CV_RGB(255,255,255),0,1,8,cv.cvPoint(0,0)); # Convert ellipse data from float to integer representation. center = cv.CvPoint() size = cv.CvSize() center.x = cv.cvRound(box.center.x); center.y = cv.cvRound(box.center.y); size.width = cv.cvRound(box.size.width*0.5); size.height = cv.cvRound(box.size.height*0.5); box.angle = -box.angle; # Draw ellipse. cv.cvEllipse(image04, center, size, box.angle, 0, 360, cv.CV_RGB(0,0,255), 1, cv.CV_AA, 0); # Show image. HighGUI use. highgui.cvShowImage( "Result", image04 );
def init(self) : self.storage=cv.cvCreateMemStorage(0)
def timerEvent(self, ev): # Fetch a frame from the video camera frame = highgui.cvQueryFrame(self.cap) img_orig = cv.cvCreateImage(cv.cvSize(frame.width,frame.height),cv.IPL_DEPTH_8U, frame.nChannels) if (frame.origin == cv.IPL_ORIGIN_TL): cv.cvCopy(frame, img_orig) else: cv.cvFlip(frame, img_orig, 0) # Create a grey frame to clarify data img_grey = cv.cvCreateImage(cv.cvSize(img_orig.width,img_orig.height), 8, 1) cv.cvCvtColor(img_orig, img_grey, cv.CV_BGR2GRAY) # Detect objects within the frame self.faces_storage = cv.cvCreateMemStorage(0) faces = self.detect_faces(img_grey) self.circles_storage = cv.cvCreateMemStorage(0) circles = self.detect_circles(img_grey) self.squares_storage = cv.cvCreateMemStorage(0) squares = self.detect_squares(img_grey, img_orig) self.lines_storage = cv.cvCreateMemStorage(0) lines = self.detect_lines(img_grey, img_orig) # Draw faces if faces: for face in faces: pt1, pt2 = self.face_points(face) cv.cvRectangle(img_orig, pt1, pt2, cv.CV_RGB(255,0,0), 3, 8, 0) # Draw lines if lines: for line in lines: cv.cvLine(img_orig, line[0], line[1], cv.CV_RGB(255,255,0), 3, 8) # Draw circles if circles: for circle in circles: cv.cvCircle(img_orig, cv.cvPoint(cv.cvRound(circle[0]),cv.cvRound(circle[1])),cv.cvRound(circle[2]),cv.CV_RGB(0,0,255),3,8,0) # Draw squares if squares: i = 0 while i<squares.total: pt = [] # read 4 vertices pt.append(squares[i]) pt.append(squares[i+1]) pt.append(squares[i+2]) pt.append(squares[i+3]) ## draw the square as a closed polyline cv.cvPolyLine(img_orig, [pt], 1, cv.CV_RGB(0,255,0), 3, cv.CV_AA, 0) i += 4 # Resize the image to display properly within the window # CV_INTER_NN - nearest-neigbor interpolation, # CV_INTER_LINEAR - bilinear interpolation (used by default) # CV_INTER_AREA - resampling using pixel area relation. (preferred for image decimation) # CV_INTER_CUBIC - bicubic interpolation. img_display = cv.cvCreateImage(cv.cvSize(self.width(),self.height()), 8, 3) cv.cvResize(img_orig, img_display, cv.CV_INTER_NN) img_pil = adaptors.Ipl2PIL(img_display) s = StringIO() img_pil.save(s, "PNG") s.seek(0) q_img = QImage() q_img.loadFromData(s.read()) bitBlt(self, 0, 0, q_img)
0, 360, _black, -1, 8, 0) cv.cvEllipse(image, cv.cvPoint(dx + 150, dy + 100), cv.cvSize(10, 5), 0, 0, 360, _black, -1, 8, 0) cv.cvEllipse(image, cv.cvPoint(dx + 150, dy + 150), cv.cvSize(40, 10), 0, 0, 360, _black, -1, 8, 0) cv.cvEllipse(image, cv.cvPoint(dx + 27, dy + 100), cv.cvSize(20, 35), 0, 0, 360, _white, -1, 8, 0) cv.cvEllipse(image, cv.cvPoint(dx + 273, dy + 100), cv.cvSize(20, 35), 0, 0, 360, _white, -1, 8, 0) # create window and display the original picture in it highgui.cvNamedWindow("image", 1) highgui.cvShowImage("image", image) # create the storage area storage = cv.cvCreateMemStorage(0) # find the contours nb_contours, contours = cv.cvFindContours(image, storage, cv.sizeof_CvContour, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, cv.cvPoint(0, 0)) # comment this out if you do not want approximation contours = cv.cvApproxPoly(contours, cv.sizeof_CvContour, storage, cv.CV_POLY_APPROX_DP, 3, 1) # create the window for the contours highgui.cvNamedWindow("contours", 1)