def detect(img_read, pt1, pt2, w): crop_img = img_read[pt1[1]:pt2[1], pt1[0]:pt2[0]] distance = conf["distance"] skp, tkp = cvlib.findKeyPoints(crop_img , target, distance) # if tkp != None: #print len(tkp) if len(tkp) > 2: save(0,0,0) #print "no" # no car else: save(0,0,1) #print "has" # has car newimg = cvlib.drawKeyPoints(img_read, target, skp, tkp, pt1, pt2, -1) else: newimg = img_read cv2.rectangle(newimg, pt1, pt2, (0,255,0)) #print "None" save(0,0,1) if DEBUG == "true": cv2.imshow("camera", newimg)
def main(): print conf target = cv2.imread(conf["app"]["target"]) #sys.argv[2]) #print type(target) #cv2.NamedWindow("camera", 1) #capture = cv2.VideoCapture(0) capture = cv2.VideoCapture(conf["app"]["camera_uri"]) i = 0 pt1 = (conf["app"]["crop_start"][0], conf["app"]["crop_start"][1]) w = conf["app"]["corp_width"] pt2 = (pt1[0] + w, pt1[1] + w) debug = 1 # conf["app"]["debug"] t1 = time.time() while True: i = i + 1 #if i > 200: # i = 0 ret, img_read = capture.read() #cv.QueryFrame(capture) # only show 1/20 if i % 12 != 0: continue pass t2 = time.time() print "[%s]" % (t2 - t1) tq = t2 if ret == False: raise (Exception("can't connect camera")) #mat=cv2.GetMat(img) #img_p = np.asarray(mat) #img_p = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U,1) #print dir(img) """ im_gray = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U,1) cv.CvtColor(img,im_gray,cv.CV_RGB2GRAY) # Sobel operator dstSobel = cv.CreateMat(im_gray.height, im_gray.width, cv.CV_32FC1) # Sobel(src, dst, xorder, yorder, apertureSize = 3) cv.Sobel(im_gray,dstSobel,1,1,3) """ #print ret try: # skp: source key points, tkp: target key points t1 = time.time() #img[200:400, 100:300] # Crop from x, y, w, h -> 100, 200, 300, 400 #im[y1:y2, x1:x2] crop_img = img_read[pt1[1]:pt2[1], pt1[0]:pt2[0]] #print(len(crop_img)) distance = conf["app"]["distance"] skp, tkp = cvlib.findKeyPoints(crop_img, target, distance) if skp == None: print("skp is none") continue else: print "==" * 20 print "time:[%.3f]" % (time.time() - t1) print "skp", len(skp) #, skp print "tkp", len(tkp) #, tkp if debug: newimg = cvlib.drawKeyPoints(img_read, target, skp, tkp, pt1, pt2, -1) cv2.imshow("camera", newimg) #gevent.sleep(1) except Exception as ex: print(ex) #gevent.sleep(3) continue #cv.ShowImage('camera', newimg) # image smoothing and subtraction # imageBlur = cv.CreateImage(cv.GetSize(im_gray), im_gray.depth, im_gray.nChannels) # # filering the original image # # Smooth(src, dst, smoothtype=CV_GAUSSIAN, param1=3, param2=0, param3=0, param4=0) # cv.Smooth(im_gray, imageBlur, cv.CV_BLUR, 11, 11) # diff = cv.CreateImage(cv.GetSize(im_gray), im_gray.depth, im_gray.nChannels) # # subtraction (original - filtered) # cv.AbsDiff(im_gray,imageBlur,diff) # cv.ShowImage('camera', diff) if cv2.waitKey(10) == 27: break
def main(): print conf target = cv2.imread(conf["app"]["target"])#sys.argv[2]) #print type(target) #cv2.NamedWindow("camera", 1) capture = cv2.VideoCapture(0) #capture = cv2.VideoCapture(conf["app"]["camera_uri"]) i = 0 pt1 = (conf["app"]["crop_start"][0],conf["app"]["crop_start"][1]) w = conf["app"]["corp_width"] pt2 = (pt1[0]+w,pt1[1]+w) debug = 1# conf["app"]["debug"] while True: i = i +1 #if i > 200: # i = 0 ret, img_read = capture.read() #cv.QueryFrame(capture) if i == 1: pass if ret == False: raise(Exception("can't connect camera")) #mat=cv2.GetMat(img) #img_p = np.asarray(mat) #img_p = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U,1) #print dir(img) """ im_gray = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U,1) cv.CvtColor(img,im_gray,cv.CV_RGB2GRAY) # Sobel operator dstSobel = cv.CreateMat(im_gray.height, im_gray.width, cv.CV_32FC1) # Sobel(src, dst, xorder, yorder, apertureSize = 3) cv.Sobel(im_gray,dstSobel,1,1,3) """ #print ret try: # skp: source key points, tkp: target key points t1 = time.time() #img[200:400, 100:300] # Crop from x, y, w, h -> 100, 200, 300, 400 #im[y1:y2, x1:x2] crop_img = img_read[pt1[1]:pt2[1], pt1[0]:pt2[0]] #print(len(crop_img)) distance = conf["app"]["distance"] skp, tkp = cvlib.findKeyPoints(crop_img , target, distance) if skp == None: print("skp is none") continue else: print "==" * 20 print "time:[%.3f]" %(time.time() - t1) print "skp", len(skp)#, skp print "tkp",len(tkp)#, tkp if debug: newimg = cvlib.drawKeyPoints(img_read, target, skp, tkp, pt1, pt2, -1) cv2.imshow("camera", newimg) #gevent.sleep(1) except Exception as ex: print(ex) #gevent.sleep(3) continue #cv.ShowImage('camera', newimg) # image smoothing and subtraction # imageBlur = cv.CreateImage(cv.GetSize(im_gray), im_gray.depth, im_gray.nChannels) # # filering the original image # # Smooth(src, dst, smoothtype=CV_GAUSSIAN, param1=3, param2=0, param3=0, param4=0) # cv.Smooth(im_gray, imageBlur, cv.CV_BLUR, 11, 11) # diff = cv.CreateImage(cv.GetSize(im_gray), im_gray.depth, im_gray.nChannels) # # subtraction (original - filtered) # cv.AbsDiff(im_gray,imageBlur,diff) # cv.ShowImage('camera', diff) if cv2.waitKey(10) == 27: break