def main(): ap = argparse.ArgumentParser("Image Stitching") ap.add_argument("-i1","--image1", required=True, help="Path to first image to be stiched") ap.add_argument("-i2","--image2", required=True, help="Path to second image to be stitched") args = vars(ap.parse_args()) imageA = cv2.imread(args["image1"]) imageB = cv2.imread(args["image2"]) imageA = imutils.resize(imageA, width=400) imageB = imutils.resize(imageB, width=400) # stitch the images together to create a panorama #SIFT stitcher stitcher = ImageStitcher() result = stitcher.stitch([imageA, imageB], showMatches=False) # cv2.imshow("Keypoint Matches", vis) cv2.imshow("Result", result) #Surf Stitcher # stitcher = SurfStitcher(imageA) # stitcher.stitch(imageB) # stitcher.saveImage() # cv2.imshow("Result", stitcher.leftImage) # show the images cv2.imshow("Image A", imageA) cv2.imshow("Image B", imageB) cv2.waitKey(0) cv2.destroyAllWindows() cv2.imwrite("results.jpg", result)
def __cammd(self): if self.__cvmode == 2: # 根据当前的工作状态智能调节相机检测阈值及灵敏度 blurratio = 21 # 高斯模糊系数 threshratio = 2 # 二值化系数(色差系数) else: blurratio = 21 threshratio = 2 (grabbed, self.__frame1) = self.__camera.read() # 获取用于对比的第一帧 time.sleep(0.1) # 两帧间隔时间 (grabbed, self.__frame2) = self.__camera.read() # 获取用于对比的第二帧 time.sleep(0.1) # 与下一次循环的间隔时间 self.__frame1 = imutils.resize(self.__frame1, width=400) # 重新定义帧大小 self.__frame2 = imutils.resize(self.__frame2, width=400) gray1 = cv2.cvtColor(self.__frame1, cv2.COLOR_BGR2GRAY) # 转换为灰阶图 gray1 = cv2.GaussianBlur(gray1, (blurratio, blurratio), 0) # 高斯模糊化灰阶图 gray2 = cv2.cvtColor(self.__frame2, cv2.COLOR_BGR2GRAY) gray2 = cv2.GaussianBlur(gray2, (blurratio, blurratio), 0) frameDelta = cv2.absdiff(gray1, gray2) # 比较两帧灰阶图像的差异 thresh = cv2.threshold(frameDelta, threshratio, 255, cv2.THRESH_BINARY)[1] # 如果两灰阶帧色差大于threshratio,则直接将其标记为白色,存入thresh图像 thresh = cv2.dilate(thresh, None, iterations=2) (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) sum = 0 for c in cnts: # 将图中所有差异部分的面积累加 area = cv2.contourArea(c) if area < 4: # 过滤面积较小的色差块,以求稳定 continue else: sum = sum + area if sum > 50: # 若差异部分面积累加大于50像素,则判定当前状态确实有人,并赋值给__temp_is_person self.__temp_is_person = True else: self.__temp_is_person = False
def process_optical_flow(video_path): cap = cv2.VideoCapture(video_path) ret, frame1 = cap.read() frame1 = imutils.resize(frame1, width=800) prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) prvs = cv2.GaussianBlur(prvs, (21, 21), 0) hsv = np.zeros_like(frame1) hsv[..., 1] = 255 while True: ret, frame2 = cap.read() frame2 = imutils.resize(frame2, width=800) next_f = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) next_f = cv2.GaussianBlur(next_f, (21, 21), 0) # flow = cv2.calcOpticalFlowFarneback(prvs, next, 0.5, 1, 3, 15, 3, 5, 1) flow = cv2.calcOpticalFlowFarneback(prvs, next_f, 0.5, 3, 15, 3, 5, 1.2, 0) mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) hsv[..., 0] = ang * 180 / np.pi / 2 hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) cv2.imshow('frame2', rgb) k = cv2.waitKey(30) & 0xff if k == ord('q'): break # elif k == ord('s'): # cv2.imwrite('opticalfb.png', frame2) # cv2.imwrite('opticalhsv.png', rgb) prvs = next_f cap.release() cv2.destroyAllWindows()
def test(name='image/last.png'): num=0 img=cv2.imread(name) filename='image/last.png' #用来比较的前一帧图片的文件名 if os.path.exists(filename): img2=cv2.imread(filename) else: cv2.imwrite(filename,img) img2=img img3=imutils.resize(img,width=500) img4=imutils.resize(img2,width=500) gray=cv2.cvtColor(img3,cv2.COLOR_BGR2GRAY) gray=cv2.GaussianBlur(gray,(5,5),0) gray2=cv2.cvtColor(img4,cv2.COLOR_BGR2GRAY) gray2=cv2.GaussianBlur(gray2,(5,5),0) frame=cv2.absdiff(gray,gray2) (h,w)=frame.shape for i in range(h): for j in range(w): if frame[i,j]>25: num+=1 if float(num)/(w*h)<0.08: #print num,w*h os.remove(name) #cv2.imshow('example',img) else: cv2.imwrite(filename,img) r = redis.Redis(host='127.0.0.1', port=6379) dir = '/home/wlw/git/pyrtsp/'+ name #保存的图片文件的路径 r.lpush('test',dir) #入队列
def center_extent(image, size): # grab the extent width and height (eW, eH) = size # handle when the width is greater than the height if image.shape[1] > image.shape[0]: image = imutils.resize(image, width = eW) # otherwise, the height is greater than the width else: image = imutils.resize(image, height = eH) # allocate memory for the extent of the image and # grab it extent = np.zeros((eH, eW), dtype = "uint8") offsetX = (eW - image.shape[1]) / 2 offsetY = (eH - image.shape[0]) / 2 extent[offsetY:offsetY + image.shape[0], offsetX:offsetX + image.shape[1]] = image # compute the center of mass of the image and then # move the center of mass to the center of the image (cY, cX) = np.round(mahotas.center_of_mass(extent)).astype("int32") (dX, dY) = ((size[0] / 2) - cX, (size[1] / 2) - cY) M = np.float32([[1, 0, dX], [0, 1, dY]]) extent = cv2.warpAffine(extent, M, size) # return the extent of the image return extent
def locate_tracker(self, debug): """ Returns the (x, y) position of the IR tracker in the camera reference plane. http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/ :return: The (x, y) position of the IR tracker in the camera reference plane. :rtype: (int, int) """ # tmp_image = # tmp_image = cv2.GaussianBlur(self.frame, (11, 11), 0) # Experiment with this hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV) # Convert to HSV Color Space. This is temporary for testing using colored objects) mask = cv2.inRange(hsv, self.hueLower, self.hueUpper) try: mask = cv2.inRange(hsv, self.hueLower2, self.hueUpper2) + mask except AttributeError: pass mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) if debug: tmpMask = imutils.resize(mask, width=1000, height=1000) cv2.imshow("mask", tmpMask) # find contours in the mask and initialize the current (x, y) center of the object cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None # only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # only proceed if the radius meets a minimum size # if radius > 10: # # draw the circle and centroid on the frame, # # then update the list of tracked points # cv2.circle(frame, (int(x), int(y)), int(radius), # (0, 255, 255), 2) # cv2.circle(frame, center, 5, (0, 0, 255), -1) if debug: cv2.drawContours(self.frame, c, -1, (0, 255, 0), 20) return center, radius # update the points queue cv2.imshow("mask", imutils.resize(mask, width=1000, height=1000)) cv2.imshow("frame", imutils.resize(self.frame, width=1000, height=1000)) cv2.waitKey(0) cv2.destroyAllWindows() raise OpenCVError("Could not find tracker!")
def ch(f,pic,bg,pt1): num=0 img1=cv2.imread(pic) img2=cv2.imread(bg) img3=imutils.resize(img1,width=500) img4=imutils.resize(img2,width=500) gray=cv2.cvtColor(img3,cv2.COLOR_BGR2GRAY) gray=cv2.GaussianBlur(gray,(5,5),0) gray2=cv2.cvtColor(img4,cv2.COLOR_BGR2GRAY) gray2=cv2.GaussianBlur(gray2,(5,5),0) frame=cv2.absdiff(gray,gray2) (h,w)=frame.shape for i in range(h): for j in range(w): if frame[i,j]>25: num+=1 if float(num)/(w*h)<0.05: os.remove(pic) else: cv2.imwrite(bg,img1) ffid = f.split('-')[1] t1=os.path.getctime(pic) t2=time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(t1)) newname=ffid+'-'+t2+'.jpg' #print newname ff1= os.path.join(pt1,newname) shutil.move(pic,ff1) fpw='/home/wlw/ffmpeg/img2' mod(newname,fpw)
def skin_detect(imagepath): # define the upper and lower boundaries of the HSV pixel # intensities to be considered 'skin' lower = np.array([0, 48, 80], dtype = "uint8") upper = np.array([20, 255, 255], dtype = "uint8") img = cv2.imread(imagepath) # convert it to the HSV color space, # and determine the HSV pixel intensities that fall into # the speicifed upper and lower boundaries frame = img frame = imutils.resize(img, width = 1000) converted = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) skinMask = cv2.inRange(converted, lower, upper) # apply a series of erosions and dilations to the mask # using an elliptical kernel kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) skinMask = cv2.erode(skinMask, kernel, iterations = 2) skinMask = cv2.dilate(skinMask, kernel, iterations = 2) # blur the mask to help remove noise, then apply the # mask to the frame skinMask = 255 - cv2.GaussianBlur(skinMask, (3, 3), 0) skinMask = imutils.resize(skinMask, height=img.shape[0], width = img.shape[1]) skinMask=cv2.resize(skinMask,(img.shape[1],img.shape[0]),interpolation=cv2.INTER_CUBIC) return skinMask
def search(self): if self.imageLink: img1 = cv2.imread(self.imageLink) img1 = imutils.resize(img1, width=1000) img1 = cv2.cvtColor(img1, cv2.COLOR_RGBA2GRAY) surf = cv2.xfeatures2d.SURF_create(2000,7,7) kp1, des1 = surf.detectAndCompute(img1, None) ans = {} for imagePath in glob.glob("logo/**/*.png"): img2 = cv2.imread(imagePath) img2 = cv2.cvtColor(img2, cv2.COLOR_RGBA2GRAY) img2 = imutils.resize(img2, width=500) kp2, des2 = surf.detectAndCompute(img2, None) bf = cv2.BFMatcher() matches = bf.knnMatch(des2, des1, k=2) good = [] for m,n in matches: if m.distance < 0.7*n.distance: good.append([m]) percent = len(good) ans.update({imagePath:percent}) percent = str(round(percent, 2)) img3 = cv2.drawMatchesKnn(img2, kp2, img1, kp1, good, None, flags=2) cv2.putText(img3, ("Good: " + str(len(good))), (10,400), 0, 1, (0,0,255),2) cv2.imwrite("static/result/" + str(os.path.basename(imagePath)), img3) if max(ans.values()) >= 20: result = str(max(ans, key=ans.get)) else: result = 'UNKNOWN' return result
def preprocess(self, image): # Grab the dimensions of the image and then intialize the deltas to use when cropping (h, w) = image.shape[:2] dW = 0 dH = 0 # If the width is smaller than the height, then resize along the width (ie the smaller dimension) # and then update the deltas to crop the height to the deired dimension if(w < h): image = imutils.resize(image, width=self.width, inter=self.inter) dH = int((image.shape[0] - self.height) / 2.0) # Otherwise the height is smaller than the width so resize along the height # and then update the deltas crop along the width else: image = imutils.resize(image, height=self.height, inter=self.inter) dW = int((image.shape[1] - self.height) / 2.0) # Now that our images have been resized, we need to regrab the width # and height, followed by performning the crop (h, w) = image.shape[:2] image = image[dH:h - dH, dW:w - dW] # Finally resize the image to the provided spatial dimensions to # ensure our output image is always a fixed size return cv2.resize(image, (self.width, self.height), interpolation=self.inter)
def stitch_images(imageA, imageB): ''' Stitches 2 images together Args: imageA: First image imageB: Second image Returns: result: the images stitched together ''' # load the two images and resize them to have a width of 400 pixels # (for faster processing) imageA = imutils.resize(imageA, width=400, height=400) imageB = imutils.resize(imageB, width=400, height=400) # stitch the images together to create a panorama stitcher = Stitcher() (result, vis) = stitcher.stitch([imageA, imageB], showMatches=True) # show the images (commented out for using pi through ssh) #cv2.imshow("Image A", imageA) #cv2.imshow("Image B", imageB) #cv2.imshow("Keypoint Matches", vis) #cv2.imshow("Result", result) #cv2.waitKey(0) return result
def test(path, newname='image/last.png', nametv='image/lasttv', namedelta='image/lastdelta'): #print 'test',name num=0 #初始化属于前景的像素个数 img=cv2.imread(nametv) filenametv=str(path[:-11])+'lasttv.png' #用来比较的前一帧图片的文件名 #print filenametv if os.path.exists(filenametv): img2=cv2.imread(filenametv) else: cv2.imwrite(filenametv,img) img2=img kernel = np.ones((5,5),np.uint8) img3=imutils.resize(img,width=500) img4=imutils.resize(img2,width=500) #img4=cv2.dilate(img4, kernel, iterations = 1) frameDelta=cv2.absdiff(img3,img4) kernel = np.ones((5,5),np.uint8) #frameDelta = cv2.erode(frameDelta, kernel, iterations = 1) #腐蚀 #print frame ''' cv2.imshow('img3',img3) cv2.imshow('img4',img4) cv2.imshow('frameDelta',frameDelta) k = cv2.waitKey(10000) & 0xff ''' (h,w)=frameDelta.shape[:2] print h,w frameDelta = cv2.threshold(frameDelta,128,255,cv2.THRESH_BINARY) #print frameDelta #print frameDelta[1] cv2.imwrite(namedelta,frameDelta[1]) for i in range(h): for j in range(w): if frameDelta[1][i,j][0]>100 and frameDelta[1][i,j][1]>100 and frameDelta[1][i,j][2]>100: num+=1 #print float(num),w,h,filename print num,w*h,(10000*num)/(w*h) if float(num)/(w*h)<0.01: #(0.001,)0.003若变化的像素个数小于图像总像素的一定比例则表示没有变化,移除该帧 #print num,w*h os.remove(newname) os.remove(nametv) os.remove(namedelta) else: os.remove(nametv) os.remove(namedelta) print 'obj:'+filenametv cv2.imwrite(filenametv,img) '''
def process_motion_frame(q, f, tick, ts, mfa=False, rotateAng=False, width=False, gBlur=(9, 9)): ''' This function defines the image processing techniques that are applied to a new thread when a frame is retreived from the camera. ''' rects_sal = [] fgmask = None f_copy = f.copy() if rotateAng is not False and rotateAng != 0: f = imutils.rotate(f, angle=rotateAng) if width is not False: f = imutils.resize(f, width=width) # blur & bg sub try: fgmask = fgbg.apply(cv2.GaussianBlur(f, gBlur, 0), learningRate=config.computing.learning_rate) except: print("-"*60) traceback.print_exc(file=sys.stdout) print("-"*60) raise # get our frame outlines f_rects, rects_mot = get_motions(f, fgmask, thickness=1) rects_sal.extend(rects_mot) num_motion = len(rects_mot) if True: # don't do anything else if there's no motion of any kind detected # if num_motion > 0 or mfa is True: num_bodies = 0 num_faces = 0 if config.computing.body_detection_en or config.computing.face_detection_en: # generate a histogram equalized bw image if we're doing processing # that needs it f_bw = cv2.equalizeHist(cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)) if config.computing.body_detection_en: fBody, rectsBody = detectPerson(f, color=(255, 0, 0)) if len(rectsBody) > 0: f_rects = cv2.add(f_rects, fBody) num_bodies = len(rectsBody) rects_sal.extend(rectsBody) if config.computing.face_detection_en: fFace, rectsFace = detectFace(f_bw, color=(0, 255, 0)) if len(rectsFace) > 0: f_rects = cv2.add(f_rects, fFace) num_faces = len(rectsFace) rects_sal.extend(rectsFace) f_rects = imutils.resize(f_rects, width=f_copy.shape[1]) q.put({"f": f_copy, "ts": ts, "rects_sal": rects_sal, "sz_scaled": getsize( f), "num_motion": num_motion, "num_bodies": num_bodies, "num_faces": num_faces}) return f_copy, f_rects, fgmask, rects_sal, tick, ts
def singleFrame(): frame1 = vs1.read() frame1 = imutils.resize(frame1, args["width"]) gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) if record == False: cv2.imshow('WebCam1: press r to record, q to quit', gray1) frame2 = vs2.read() frame2 = imutils.resize(frame2, args["width"]) gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) if record == False: cv2.imshow('WebCam2: press r to record, q to quit', gray2) return gray1, gray2
def upload(): # Get the name of the uploaded file file = request.files['file'] # Check if the file is one of the allowed types/extensions if file and allowed_file(file.filename): # Make the filename safe, remove unsupported chars filename = secure_filename(file.filename) print file.filename # Move the file form the temporal folder to # the upload folder we setup file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # Redirect the user to the uploaded_file route, which # will basicaly show on the browser the uploaded file #print '\n\nI am Here\n\n' total = frameCapture('uploads/'+file.filename) min_match_count = 7 img1 = cv2.imread('images/1.jpg',0) img1 = binarize(img1) img1 = imutils.resize(img1, width=1000) for i in range(2,(total)): print 'Stitching image ' + str(i) img2 = cv2.imread('images/'+str(i)+'.jpg',0) img2 = binarize(img2) img2 = imutils.resize(img2, width=1000) img1 = stitch(img1, img2, min_match_count) cv2.imwrite('images/0.jpg',img1) cv2.imwrite('static/0.jpg',img1) print '\n\nConverting Image to Text' string = pytesseract.image_to_string(Image.open('images/0.jpg')) print '\n\nOCR OUTPUT\n\n' + string + '\n\n' f = open("static/test.txt","w") f.write(string) f.close() string = '"{}"'.format(string) print 'Converting Text to Speech\n\n' tts = gTTS(text=string, lang='en') tts.save("static/tts.mp3"); return render_template('index1.html')
def fix_perspective(image): image = cv2.imread(image) ratio = image.shape[0]/500.0 original = image.copy() image = imutils.resize(image, height = 500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5,5),0) edged = cv2.Canny(gray, 75, 200) (cnts,_) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key = cv2.contourArea, reverse =True)[:5] for contour in cnts: peri = cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, 0.02 * peri, True) if len(approx) == 4: screenCnt = approx break warped = corner_transformation(original, screenCnt.reshape(4,2)*ratio) cv2.imwrite('check.JPG', warped)
def saveGoalVideo(): goalOutStream = cv2.VideoWriter('C:/Users/moshec/Documents/InteractiveFoosball-Hackaton/Foos-Python/ball-tracking/ball-tracking/Output/output_Goal.avi', 0, 30.0, (600,420)) for f in replayFrames: new_f = imutils.resize(f, width=600) new_f = new_f[0:420,0:600] goalOutStream.write(new_f) goalOutStream.release()
def main(): global frame global a, b, c, d bytes_data = '' camera = cv2.VideoCapture(0) clientSocket = set_connection() # clientThread = ClientThread(name="ClientThread", clientSocket = clientSocket) k = 0 limiter = 8 while True: try: time.sleep(0.2) _, nparr = camera.read() #img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR) frame = imutils.resize(nparr, width = 300) (frame, platform_center, object_center)= detect_object(frame) k = k + 1 if k % limiter == 0: print('going back tok balanced state') balancedState() #clientThread.start() #print('Platform center = {0}, Object center = {1}'.format(platform_center, object_center)) #quadrant = determine_quadrant(platform_center, object_center) #print('The object lies in quadrant = {0}'.format(quadrant)) # hack for now. # if platform_center == None and object_center!=None: picture_size = () picture_size = get_frame_size(frame) platform_center= (picture_size[0]/2+30, picture_size[1]/2+20) cv2.circle(frame, platform_center, int(convert_cm_to_pixels(3)), (0,255,0), 3) send_image(clientSocket, frame) #print('The platform center = {0}'.format(platform_center)) if platform_center!=None and object_center!=None: print('found the ball on the platform') platform_cm = convert_point_to_cm(platform_center) object_cm = convert_point_to_cm(object_center) if abs(platform_cm[0] - object_cm[0]) > 9: limiter = 10 else: limiter = 4 #distance_cm = calculate_distance(platform_cm, object_cm) #print("coordinates, platform_center = {0}, object center = {1}".format(platform_center, object_center)) #print('platform center = {0}, object center = {1}'.format(platform_cm, object_cm)) #print('The distance(cm) = {0}'.format(distance_cm)) #print("coordinate one ={0}, and coordinate two = {1}".format(object_cm[0]-platform_cm[0],object_cm[1]-platform_cm[1])) t = MyThread(name = "Thread - {0}".format("Calc and run servos"), x_distance=object_cm[0]-platform_cm[0], y_distance =object_cm[1]-platform_cm[1]) t.start() #set_servo_angles(object_cm[0]-platform_cm[0], object_cm[1]-platform_cm[1]) #MoveServos() #MoveServos() #cv2.imshow("result", frame) #cv2.waitKey(1) else: print('Cannot find the ball') #balancedState() # move to original state. except cv2.error as e: print('there was an exception') print(e)
def videoLoop(self): # DISCLAIMER: # I'm not a GUI developer, nor do I even pretend to be. This # try/except statement is a pretty ugly hack to get around # a RunTime error that throws due to threading try: # keep looping over frames until we are instructed to stop while not self.stopEvent.is_set(): # grab the frame from the video stream and resize it to # have a maximum width of 300 pixels self.frame = self.vs.read() self.frame = imutils.resize(self.frame, width=300) # represents images in BGR order; however PIL # represents images in RGB order, so we need to swap # the channels, then convert to PIL and ImageTk format image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(image) image = ImageTk.PhotoImage(image) # if the panel is not None, we need to initialize it if self.panel is None: self.panel = tki.Label(image=image) self.panel.image = image self.panel.pack(side="left", padx=10, pady=10) # otherwise, simply update the panel else: self.panel.configure(image=image) self.panel.image = image except RuntimeError as e: print("[INFO] caught a RuntimeError")
def main(): bytes_data = '' camera = cv2.VideoCapture(0) while True: try: _, nparr = camera.read() #img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR) frame = imutils.resize(nparr, width = 300) (frame, platform_center, object_center)= detect_object(frame) cv2.imwrite("platformImage.jpg", frame) print('Platform center = {0}, Object center = {1}'.format(platform_center, object_center)) quadrant = determine_quadrant(platform_center, object_center) print('The object lies in quadrant = {0}'.format(quadrant)) if platform_center!=None and object_center!=None: distance = calculate_distance(platform_center, object_center) print('The distance (pixels) = {0}'.format(distance)) platform_cm = convert_point_to_cm(platform_center) object_cm = convert_point_to_cm(object_center) distance_cm = calculate_distance(platform_cm, object_cm) print('The distance(cm) = {0}'.format(distance_cm)) # print('Distance in cm = {0}'.format(in_cm)) set_servo_angles(object_center[0]-platform_center[0], object_center[1]-platform_center[1]) print('Now rotating the servos...') MoveServos() #cv2.imshow("result", frame) #cv2.waitKey(1) except cv2.error as e: print('there was an exception') print(e)
def captureRawFrame(self): """ capture frame and reverse RBG BGR and return opencv image """ rawFrame = self.vs.read() rawFrame = imutils.resize(rawFrame, width=640) self.raw_img = rawFrame
def task(self): self.next_frame() gray = cv2.cvtColor(self.clean_image, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, self.canny_threshold1.get(), self.canny_threshold2.get(), apertureSize = ((self.canny_apertureSize.get()*2) - 1) ) lines = cv2.HoughLines(edges, self.rho.get(), np.pi/self.theta.get(), self.threshold.get() ) if lines != None: for line in lines: rho_x,theta_x = line[0] a = np.cos(theta_x) b = np.sin(theta_x) x0 = a*rho_x y0 = b*rho_x x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) cv2.line(self.clean_image,(x1,y1),(x2,y2),(0,0,255),2) self.clean_image = imutils.resize(self.clean_image, width=1000) cv2.imshow(self.window_name, self.clean_image) self.master.after(20, self.task)
def beginVideoProcess(im): # load the image and resize it to (1) reduce detection time # and (2) improve detection accuracy image = im.copy() orig = image.copy() #image = cv2.imread(imagePath) image = imutils.resize(image, width=min(400, image.shape[1])) # detect people in the image (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05) # draw the original bounding boxes for (x, y, w, h) in rects: cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2) # apply non-maxima suppression to the bounding boxes using a # fairly large overlap threshold to try to maintain overlapping # boxes that are still people rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) # draw the final bounding boxes for (xA, yA, xB, yB) in pick: cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) # show some information on the number of bounding boxes filename = "webcam" print("[INFO] {}: {}".format(filename, len(pick))) # show the output images cv2.imshow("After NMS", image) return len(pick)
def lipSegment(img): img = imutils.resize(img,width=300) img_copy = img.copy() landmarks = dlib_obj.get_landmarks(img) dlib_obj.get_face_mask(img_copy, landmarks) output_img = img-img_copy output_img = cv2.cvtColor(output_img,cv2.COLOR_BGR2GRAY) contours,hierarchy = cv2.findContours(output_img.copy(), cv2.cv.CV_RETR_EXTERNAL, cv2.cv.CV_CHAIN_APPROX_SIMPLE) #cv2.findContours(image, mode, method cv2.drawContours(img, contours, -1, (0,255,0), 2,maxLevel=0) cnt = contours[0] ellipse = cv2.fitEllipse(cnt) (x,y),(MA,ma),angle = cv2.fitEllipse(cnt) a = ma/2 b = MA/2 eccentricity = sqrt(pow(a,2)-pow(b,2)) eccentricity = round(eccentricity/a,2) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img,'Eccentr= '+str(round(eccentricity,3)),(10,350), font, 1,(255,0,0),2,16) if(eccentricity < 0.9): cv2.putText(img,'Commands = O',(10,300), font, 1,(0,0,255),2,16) else: cv2.putText(img,'Commands = E',(10,300), font, 1,(0,0,255),2,16) return img
def initialize(video_capture,rot_angle, pt1, pt2, ppl_width): #read image ret, image = video_capture.read() (hh, ww) = image.shape[:2] #rotate M = None; if (rot_angle != 0): center = (ww / 2, hh / 2) M = cv2.getRotationMatrix2D(center, rot_angle, 1.0) image = imutils.resize(image, width=min(400, image.shape[1])) ##mask after resize resize_ratio = image.shape[1] / float(ww) #max_min_ppl_size ppl_size=[50,100] ppl_size[0] = np.ceil(ppl_width * resize_ratio * 1.4) ppl_size[1] = np.ceil(ppl_width * resize_ratio * 0.8) #print max_ppl_size ROI_1 = np.int_(np.dot(pt1,resize_ratio)) ROI_2 = np.int_(np.dot(pt2,resize_ratio)) return [ww, hh, M, ppl_size, ROI_1, ROI_2]
def predict(args): # load the trained convolutional neural network print("[INFO] loading network...") model = load_model(args["model"]) #load the image image = cv2.imread(args["image"]) orig = image.copy() # pre-process the image for classification image = cv2.resize(image, (norm_size, norm_size)) image = image.astype("float") / 255.0 image = img_to_array(image) image = np.expand_dims(image, axis=0) # classify the input image result = model.predict(image)[0] #print (result.shape) proba = np.max(result) label = str(np.where(result==proba)[0]) label = "{}: {:.2f}%".format(label, proba * 100) print(label) if args['show']: # draw the label on the image output = imutils.resize(orig, width=400) cv2.putText(output, label, (10, 25),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) # show the output image cv2.imshow("Output", output) cv2.waitKey(0)
def find_waldo(puzzle_image, waldo_image): ''' Takes a puzzle image and an image of waldo, and returns his location in the puzzle. ''' puzzle = cv2.imread(puzzle_image) waldo = cv2.imread(waldo_image) (waldo_height, waldo_width) = waldo.shape[:2] # find waldo. TMCC0EFF is the matching method used. result = cv2.matchTemplate(puzzle, waldo, cv2.TM_CCOEFF) (_, _, minLoc, maxLoc) = cv2.minMaxLoc(result) top_left = maxLoc bot_right = (top_left[0] + waldo_width, top_left[1] + waldo_height) roi = puzzle[top_left[1]:bot_right[1], top_left[0]:bot_right[0]] # construct a darkened transparent layer for everything except waldo. mask = np.zeros(puzzle.shape, dtype = 'uint8') puzzle = cv2.addWeighted(puzzle, 0.25, mask, 0.75, 0) # put original waldo in to look brighter than rest of image puzzle[top_left[1]:bot_right[1], top_left[0]:bot_right[0]] = roi cv2.imshow('Puzzle', imutils.resize(puzzle, height = 650)) cv2.imshow('Waldo', waldo) cv2.waitKey(0)
def analyze(image): image = imutils.resize(image, width=min(400, image.shape[1])) #orig = image.copy() # detect people in the image (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05) # draw the original bounding boxes #for (x, y, w, h) in rects: #cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2) # apply non-maxima suppression to the bounding boxes using a # fairly large overlap threshold to try to maintain overlapping # boxes that are still people rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) # draw the final bounding boxes #for (xA, yA, xB, yB) in pick: # cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) # show some information on the number of bounding boxes #filename = imagePath[imagePath.rfind("/") + 1:] #print("[INFO] {}: {} original boxes, {} after suppression".format( # filename, len(rects), len(pick))) # show the output images #cv2.imshow("Before NMS", orig) #cv2.imshow("After NMS", image) #cv2.waitKey(0) return pick
def moution_detect(self, prev_frame): """ Detect moution on stream """ frame = self.read_frame() # make grayscale frame frame = imutils.resize(frame, width=self.scale) grays = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blure = cv2.GaussianBlur(grays, (11, 11), 0) if prev_frame is None: return frame, [blure, ] * self.frame_memory prev_frame = prev_frame[:self.frame_memory] frame_delta = cv2.absdiff(prev_frame[-1], grays) thresh = cv2.threshold(frame_delta, 20, 255, cv2.THRESH_BINARY)[1] dil = cv2.dilate(thresh, np.ones((7, 7), np.uint8)) contours, hierarchy = cv2.findContours(dil.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: if cv2.contourArea(contour) < self.min_area: continue (x, y, w, h) = cv2.boundingRect(contour) cv2.rectangle(frame, (x, y), (x + w, y + h), self.contour_color, self.contour_width) sub_img = frame[y:y + h, x:x + w] x = self.hist_compare(sub_img, prev_frame[-1][y:y + h, x:x + w]) # self.draw_hist(sub_img) # return sub_img, blure return frame, blure
def area_based_track(self, frame): # 3. count # of moved pixels to compute occupancy # 3a. subsampling box = conf.TARGET_AREA (H, W) = frame.shape[:2] if conf.SUBSAMPLING_SCALE > 1: frame = imutils.resize(frame, width=W/conf.SUBSAMPLING_SCALE) box = list(map(lambda (x,y): (x/conf.SUBSAMPLING_SCALE, y/conf.SUBSAMPLING_SCALE), box)) (H, W) = frame.shape[:2] box_height = box[1][1] - box[0][1] box_width = box[1][0] - box[0][0] box_area = box_height * box_width # 3b. check sudden change if conf.CHECK_SUDDEN_CHANGE and cv2.countNonZero(frame) > (W*H) * conf.SUDDEN_CHANGE_RATIO: print("Sudden change detected") self.flush_objects() return [] # 3c. Split frame into 3 parts and compute occupancy of each count_lst = [] for i in range(3): y_pos = box[0][1] + box_height*i/3 f = frame[y_pos:y_pos + box_height/3, box[0][0]:box[1][0]] count_lst.append(cv2.countNonZero(f)) occ_lst = list(map(lambda c: (c / float(box_area/3)) > conf.OCCUPIED_RATIO, count_lst)) #print(count_lst[0], count_lst[1], count_lst[2]) #print(occ_lst) # 4. Check crossed objects self.cur_in = False self.cur_out = False if self.occ_lst is not None and self.iteration - self.last_report_iter > conf.MIN_REPORT_INTERVAL: if self.occ_lst[1] == True and occ_lst[1] == False: if any(occ_lst): self.checkout_occ = (self.iteration, occ_lst, list(self.occ_history)) else: # quick change -> Use previous frame self.checkout_occ = (self.iteration, list(self.occ_lst), list(self.occ_history)) # After noisy frames if self.checkout_occ is not None and self.iteration - self.checkout_occ[0] > conf.MIN_NOISY_INTERVAL: oc = self.checkout_occ[1] oh = self.checkout_occ[2] #print("!!!", oc, oh) if oc[0] == True and self.occ_in_history(oh, 2): self.cur_in = True if oc[2] == True and self.occ_in_history(oh, 0): self.cur_out = True if self.cur_in or self.cur_out: self.last_report_iter = self.checkout_occ[0] #print("C", self.cur_in, self.cur_out) self.checkout_occ = None self.occ_lst = occ_lst self.occ_history.append(occ_lst) return []
if (img_flip_v is True): frame = cv2.flip(frame, 0, dst=None) if (i % 4 == 3): bg = cv2.imread("bg_main_detect.png") elif (i % 4 == 0): bg = cv2.imread("bg_main_light.png") elif (i % 4 == 1): bg = cv2.imread("bg_main_water.png") elif (i % 4 == 2): bg = cv2.imread("bg_main_ndvi.png") now_light = read_light() now_water = read_soil() bg = plotLine(bg, now_light, now_water) frame_display = imutils.resize(frame, width=img_display_resize_w) #bg[12:12+frame_display.shape[0], 12:12+frame_display.shape[1]] = frame_display if (i % 4 == 2): frame_display = ndvi(frame_display) bg[bg.shape[0] - frame_display.shape[0] - 15:bg.shape[0] - 15, 12:12 + frame_display.shape[1]] = frame_display if (i % 4 == 3): final_display = draw_plant_box(bg) elif (i % 4 == 0): final_display = light_control(bg, now_light) elif (i % 4 == 1): final_display = water_control(bg, now_water) elif (i % 4 == 2): final_display = bg
def roi_creation(): global roi_coord global ix, iy, vx, vy, mouse_flag if os.path.isfile("roi_line_config.txt"): try: file1 = open("roi_line_config.txt", "r+") t = file1.read() # print(t.split("\n")) roi_coord = t.split("\n") line_type = roi_coord[-2] # print(roi_coord) i = 0 except IndexError: file9 = open("error_logs.txt", "w") file9.write( "list index out of range...i.e.roi selection is not done propperly values missing..delete roi_line_config.txt and run again..." ) print("failure") path = str(current_dir) file_name = "roi_line_config.txt" remove_file(path, file_name) exit(0) print("success") else: ix, iy = -1, -1 vx, vy = 1, 1 mouse_down = False mouse_flag = 0 x = y = w = h = 0 line_type = '' if cam == str(0) or cam == str(1): cap = cv2.VideoCapture(int(cam)) else: cap = cv2.VideoCapture(str(cam)) rectangle_flag = 0 print( " 'r' for rect roi selection , l for line ,'q' for exit selection mode" ) x = y = w = h = 0 rectangle_flag = 0 line_flag = 0 file1 = open("roi_line_config.txt", "w") while (True): # to select roi loop # Capture frame-by-frame ret, frame = cap.read() frame_new = frame frame = imutils.resize(frame, width=800) frame_new = imutils.resize(frame, width=800) key = cv2.waitKey(1) & 0xFF if key == ord("r") or key == ord("R"): initBB = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True) # print(initBB) ll = list(initBB) x = initBB[0] y = initBB[1] w = initBB[2] h = initBB[3] roi_coord.insert(0, initBB[0]) roi_coord.insert(1, initBB[1]) roi_coord.insert(2, initBB[2]) roi_coord.insert(3, initBB[3]) cv2.destroyAllWindows() while True: if roi_coord[0] == '0' and roi_coord[ 1] == '0' and roi_coord[2] == '0': print("Please Select ROI properly...") initBB = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True) ll = list(initBB) x = initBB[0] y = initBB[1] w = initBB[2] h = initBB[3] roi_coord.insert(0, initBB[0]) roi_coord.insert(1, initBB[1]) roi_coord.insert(2, initBB[2]) roi_coord.insert(3, initBB[3]) else: break rectangle_flag = 1 file1.write(str(initBB[0]) + "\n") file1.write(str(initBB[1]) + "\n") file1.write(str(initBB[2]) + "\n") file1.write(str(initBB[3]) + "\n") if key == ord("l"): # cv2.imshow('frame_new', frame) cv2.destroyAllWindows() line_type = input( "enter lineType 'h' for Horizontal,'v' for vertical..\n") print(line_type) while True: print_flag = 1 if (line_type != 'h' and line_type != 'v'): if print_flag == 1: print( "invalid_cradentials,reenter line credentials press l.. " ) line_type = input( "enter lineType 'h' for Horizontal,'v' for vertical..\n" ) print_flag = 0 elif (line_type == 'h' or line_type == 'v'): break print("\n") cv2.destroyAllWindows() # cv2.setMouseCallback('frame', line) roi_coord.insert(10, line_type) roi_coord.insert(9, line_type) mouse_flag = 2 if key == ord("q"): break if rectangle_flag == 1: # cv2.rectangle(frame,(384,0),(510,128),(0,255,0),3) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) # framei= frame[y:y + h, x:x + w] if mouse_flag == 2: if line_type == 'v': cv2.line(frame, ((x + x + w) // 2, y), ((x + x + w) // 2, y + h), (255, 0, 0), 5) if line_type == 'h': cv2.line(frame, (x, (y + y + h) // 2), (x + w, (y + y + h) // 2), (255, 0, 0), 5) # cv2.line(frame, (0, 0), (511, 511), (255, 0, 0), 5) cv2.putText(frame, "first press r to select ROI ..", (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 200), 2, cv2.LINE_AA) cv2.putText(frame, "then l to draw line..", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 200), 2, cv2.LINE_AA) cv2.putText( frame, "q to quit selection mode...(DO NOT QUIT WITHOUT SELECTION) ...", (10, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 200), 2, cv2.LINE_AA) cv2.imshow('frame', frame) print_flag = 1 # file1.write(str(x) + "\n") file1.write(str((y + y + h) // 2) + "\n") file1.write(str(x + w) + "\n") file1.write(str((y + y + h) // 2) + "\n") file1.write(str(line_type) + "\n") file1.close() # When everything done, release the capture cap.release() cv2.destroyAllWindows()
from imutils import paths import numpy as np import argparse import imutils import pickle import cv2 import os protoPath = "face_detection_model/deploy.prototxt" modelPath = "face_detection_model/res10_300x300_ssd_iter_140000.caffemodel" detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) image = cv2.imread("dataset/adrian/00000.png") image = imutils.resize(image, width=600) (h, w) = image.shape[:2] # construct a blob from the image imageBlob = cv2.dnn.blobFromImage( cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) detector.setInput(imageBlob) detections = detector.forward() print(detections)
def main(): # initialize the list of class labels MobileNet SSD was trained to # detect, then generate a set of bounding box colors for each class CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe('MobileNetSSD_deploy.prototxt.txt', 'MobileNetSSD_deploy.caffemodel') # initialize the video stream, allow the cammera sensor to warmup, # and initialize the FPS counter print("[INFO] starting video stream...") vs = VideoStream(src=3).start() time.sleep(2.0) fps = FPS().start() # loop over the frames from the video stream while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=800) # grab the frame dimensions and convert it to a blob (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) # pass the blob through the network and obtain the detections and # predictions net.setInput(blob) detections = net.forward() # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence > 0.2: # extract the index of the class label from the # `detections`, then compute the (x, y)-coordinates of # the bounding box for the object idx = int(detections[0, 0, i, 1]) box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # draw the prediction on the frame label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100) cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
# construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-m", "--model", required=True, help="path to trained model model") ap.add_argument("-l", "--labelbin", required=True, help="path to label binarizer") ap.add_argument("-i", "--image", required=True, help="path to input image") args = vars(ap.parse_args()) # load the image image = cv2.imread(args['image']) output = imutils.resize(image, width=400) # pre-process image for classification in the same manner as training data image = cv2.resize(image, (96, 96)) image = image.astype("float") / 255.0 image = img_to_array(image) image = np.expand_dims(image, axis=0) # load the trained convolutional neural network and the multi-label binarizer print("Loading network...") model = load_model(args['model']) mlb = pickle.loads(open(args['labelbin'], "rb").read()) # We classify the (preprocessed) input image and extract the top two class labels indices by # Sorting the array indexes by their associated probability in descending order # Grabbing the first two class label indices which are thus the top-2 predictions from our network
[0, 0], [maxWidth - 1, 0], [maxWidth - 1, maxHeight - 1], [0, maxHeight - 1]], dtype = "float32") M = cv2.getPerspectiveTransform(rect, dst) warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight)) return warped # ap = argparse.ArgumentParser() # ap.add_argument('-i','--image',required=True,help="Path to image file") # # args = vars(ap.parse_args()) #flag: 获取了图像路径 image = cv2.imread('test.jpg') image = imutils.resize(image,height=500) gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) #灰度图像 # gray = cv2.GaussianBlur(gray,(5,5),0) #高斯模糊 edged = cv2.Canny(gray,75,200) #边缘检测 # flag : Test1 = BLOCK print("STEP 1 Edge Detection") cv2.imshow("Edge",edged) cv2.waitKey(0) cv2.destroyAllWindows()
transfer = cv2.merge([l, a, b]) transfer = cv2.cvtColor(transfer.astype("uint8"), cv2.COLOR_LAB2BGR) return transfer def image_stats(image): (l, a, b) = cv2.split(image) (lMean, lStd) = (l.mean(), l.std()) (aMean, aStd) = (a.mean(), a.std()) (bMean, bStd) = (b.mean(), b.std()) return (lMean, lStd, aMean, aStd, bMean, bStd) import argparse import imutils ap = argparse.ArgumentParser() ap.add_argument("--source", help="path to source image file", required=True) ap.add_argument("--target", help="path to target image file", required=True) args = vars(ap.parse_args()) source = cv2.imread(args["source"]) target = cv2.imread(args["target"]) transfer = color_transfer(source, target) cv2.imshow("colour transfer", np.hstack([imutils.resize(source, height=400), imutils.resize(target, height=400), imutils.resize(transfer, height=400)])) cv2.waitKey(0)
searchParams=dict(checks=50) flann=cv2.FlannBasedMatcher(indexParams,searchParams) ratio_l=[] vis_l=[] applno_l=[] result = [] outputPath='./Output/' #path of database tmpfiles = os.listdir(outputPath) for f in tmpfiles: os.remove('./Output/'+str(f)) sampleImage = cv2.imread(samplePath,0) sampleImage1 = imutils.resize(sampleImage, width = 300) sampleImage1 = cv2.GaussianBlur(sampleImage1, (5, 5), 0) kp1_1, des1_1 = sift.detectAndCompute(sampleImage1, None) #detect the features of sample sampleImage = cv2.imread(samplePath,0) sampleImage2 = imutils.resize(sampleImage, width = 300) sampleImage2 = cv2.GaussianBlur(sampleImage2, (1, 1), 0) ret,sampleImage2 = cv2.threshold(sampleImage2,220,255,cv2.THRESH_BINARY) #sampleImage = cv2.Canny(sampleImage, 30, 150) kp1_2, des1_2 = sift.detectAndCompute(sampleImage2, None) #detect the features of sample index = 0 for t in tmark_l: index = index + 1 print(index) f = t['file'] queryImage=cv2.imread(f,0)
# keep looping while True: # grab the current frame (grabbed, frame) = camera.read() # if we are viewing a video and we did not grab a frame, # then we have reached the end of the video if args.get("video") and not grabbed: break # capture frames from the camera #for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # resize the frame, blur it, and convert it to the HSV # color space # frame = imutils.resize(frame.array, width=600) # for picamera frame = imutils.resize(frame, width=600) # for video # blurred = cv2.GaussianBlur(frame, (11, 11), 0) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # Threshold the HSV image to get only cyan colors maskTable = cv2.inRange(hsv, lower_cyan, upper_cyan) maskTable = cv2.dilate(maskTable, None, iterations=10) maskTable = cv2.erode(maskTable, None, iterations=10) # Threshold the HSV image for white color maskWhite = cv2.inRange(hsv, lower_white, upper_white) # maskWhite = cv2.dilate(maskWhite, None, iterations=2) # maskWhite = cv2.erode(maskWhite, None, iterations=2) #maskWhite = cv2.morphologyEx(maskWhite, cv2.MORPH_OPEN, kernel) maskWhite = maskWhite & maskTable
@author: philipp """ import cv2 import imutils as im import glob import os.path import numpy as np import matplotlib.pyplot as plt import pandas as pd import seaborn as sns #Import test image and create versions of images need for later steps test_fp = 'C:\\Users\\guest.IR1171\\Dropbox\\Masterarbeit\\Experiment images\\400 lmin\\DSC_0007.jpg' test_im = cv2.imread(test_fp, -1) test_im = im.resize(test_im, width=600) test_im_alpha = test_im[:, :, 2] test_im_greyscale = cv2.cvtColor(test_im, cv2.COLOR_BGR2GRAY) test_im_lab = cv2.cvtColor(test_im, cv2.COLOR_BGR2LAB) lum, green_red, test_im_blue_yellow = cv2.split(test_im_lab) ret_val, thresh_im = cv2.threshold(test_im_blue_yellow, 80, 255, cv2.THRESH_BINARY_INV) #Convert the from BGR colourspace to LAB colour space, split channels and use #the blue/yellow channel for dye extract as this is where the great contrast between #blue dye and yellow sand can be seen. rgba = cv2.cvtColor(test_im, cv2.COLOR_BGR2RGBA) images = [rgba, test_im_alpha, test_im_greyscale, test_im_blue_yellow] titles = ["original", "alpha", "greyscale", "blue/yellow greyscale"] fig, axes = plt.subplots(1, len(images))
while True: # grab the current frame frame = vs.read() # handle the frame from VideoCapture or VideoStream frame = frame[1] if args.get("video", False) else frame # if we are viewing a video and we did not grab a frame, # then we have reached the end of the video if frame is None: break # resize the frame, blur it, and convert it to the HSV # color space # print(frame.shape) # original (640, 1152, 3) frame = imutils.resize(frame, width=600) cv2.flip(frame, 0) # print(frame.shape) # resized (333, 600, 3) blurred = cv2.GaussianBlur(frame, (11, 11), 0) hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) # construct a mask for the color "green", then perform # a series of dilations and erosions to remove any small # blobs left in the mask mask = cv2.inRange(hsv, greenLower, greenUpper) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) # find contours in the mask and initialize the current # (x, y) center of the ball cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
def process_video(video): config = get_hyperparam_obj() data, workoout_date = init_workout() model = load_model(args["model"]) pts = deque(maxlen=args["buffer"]) fps_time = 0 all_ys, output_list, labels = [], [], [] sets = {} label = "" wait_for_movement = True first_pred = True last_time = 0 first_pass = True frame_buffer = deque([]) while True: (grabbed, frame) = camera.read() if args.get("video") and not grabbed: # reached end of video update(all_ys, label, sets, data, workoout_date, output_list, labels) update_json(data) break frame_buffer = append_to_frame_buffer(frame_buffer, frame, config) # frame_buffer = deque(frame_buffer) # output = frame.copy() # output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB) # output = cv2.resize( # output, (config.width, config.height)).astype("float32") # # output /= 255. # frame_buffer.appendleft(output) # if len(frame_buffer) > config.seq_len: # frame_buffer.pop() # frame_buffer = np.array(frame_buffer) if first_pass and len(frame_buffer) == 64: print(frame_buffer.shape) curr_time = int(time.time()) if curr_time - last_time > 2: last_time = curr_time label = predict_frame(model, frame_buffer, labels, config) first_pass = False frame = imutils.resize(frame, width=432, height=368) pts = track_point(frame, config, pts) # hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # # # construct a mask for the color and remove any small # # blobs left in the mask # mask = cv2.inRange(hsv, colorLower, colorUpper) # mask = cv2.erode(mask, None, iterations=2) # mask = cv2.dilate(mask, None, iterations=2) # # # find contours in the mask and initialize the current # # (x, y) center of the barbell # contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, # cv2.CHAIN_APPROX_SIMPLE)[-2] # center = None # # # only proceed if at least one contour was found # if len(contours) > 0: # # find the largest contour in the mask, then use # # it to compute the minimum enclosing circle and # # centroid # largest_contour = max(contours, key=cv2.contourArea) # ((x, y), radius) = cv2.minEnclosingCircle(largest_contour) # M = cv2.moments(largest_contour) # if M["m00"] != 0: # cX = int(M["m10"] / M["m00"]) # cY = int(M["m01"] / M["m00"]) # else: # cX, cY = 0, 0 # center = (cX, cY) # # # draw points if radius is large enough # if radius > 10: # cv2.circle(frame, (int(x), int(y)), int(radius), # (0, 255, 255), 2) # cv2.circle(frame, center, 5, (0, 0, 255), -1) # # # update the points queue # pts.appendleft(center) # keep track of all non None points and trim off excess horizontal movement if pts[0] is not None and pts[1] is not None: if abs(pts[0][0] - pts[1][0]) < abs(pts[0][1] - pts[1][1]) and abs(pts[0][1] - pts[1][1]) < 3: all_ys.append(pts[0]) pts_no_nones = [i for i in pts if i] if not all(v is None for v in pts): if (is_still(pts_no_nones)): # Object is still if not wait_for_movement: update(all_ys, label, sets, data, workoout_date, output_list, labels) labels = [] all_ys = [] wait_for_movement = True else: # Object is moving only predict when obj is moving if not args["predict_each_frame"] and vertical_movement(pts_no_nones) and not horizontal_movement(pts_no_nones) and len(frame_buffer) == config.seq_len: curr_time = int(time.time()) if curr_time - last_time > 2: last_time = curr_time label = predict_frame(model, frame_buffer, labels, config) if first_pred: for i in range(50): print("\n") print("Logging workout for %s" % (workoout_date)) first_pred = False wait_for_movement = False if args["predict_each_frame"] and len(frame_buffer == 64): curr_time = int(time.time()) if curr_time - last_time > 2: last_time = curr_time label = predict_frame(model, frame_buffer, labels, config) if first_pred: for i in range(50): print("\n") print("Logging workout for %s" % (workoout_date)) first_pred = False # loop over the set of tracked points and draw connecting lines for tracing for i in range(1, len(pts)): if pts[i - 1] is None or pts[i] is None: continue thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5) cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness) frame = add_label_and_fps(frame, fps_time, label) fps_time = time.time() cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # press q to exit loop and update json if key == ord("q"): update_json(data) break return data
def main(): # initialize frame dimensions, resized frame dimensions, and the ratio between them (W, H) = (None, None) (newW, newH) = (320, 320) # needs to be multiples of 32 (rW, rH) = (None, None) # layer names that will be used, one for output probabilities and the other for box coordinates layerNames = ["feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"] # laod the text detector net = cv2.dnn.readNet('frozen_east_text_detection.pb') # start the video stream vs = VideoStream(src=0).start() #vs = cv2.VideoCapture('test_video.mp4') # start fps calculator fps = FPS().start() fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') out = cv2.VideoWriter('video_demo.avi', fourcc, 10, (1000, 562)) # video processing loop while True: frame = vs.read() # get a frame frame = frame[1] if frame is None: break # resize the frame frame = imutils.resize(frame, width=1000) frame_original = frame.copy() if W is None or H is None: (H, W) = frame.shape[:2] print(H, W) rW = W / float(newW) rH = H / float(newH) frame = cv2.resize(frame, (newW, newH)) # create a blob and use it for text detection blob = cv2.dnn.blobFromImage(frame, 1.0, (newW, newH), (123.68, 116.78, 103.94), swapRB=True, crop=False) net.setInput(blob) (scores, geometry) = net.forward(layerNames) # get bounding boxes for predictions that pass the minimum confidence requirement (rects, confidences) = decode_predictions(scores, geometry) # eliminate unnecessary overlapping boxes boxes = non_max_suppression(np.array(rects), probs=confidences) # loop over each box for (startX, startY, endX, endY) in boxes: # 2 px buffer added to each side of each box startX = int(startX * rW - 2) startY = int(startY * rH - 2) endX = int(endX * rW + 2) endY = int(endY * rH + 2) cv2.rectangle(frame_original, (startX, startY), (endX, endY), (0, 255, 0), 2) # crop the image to just the bounding box image_cropped = frame_original[startY:endY, startX:endX] # uncomment the line below to detect black text instead of white text #image_cropped = cv2.bitwise_not(image_cropped) # attempt preprocessing on the image try: image_gray = cv2.cvtColor(image_cropped, cv2.COLOR_BGR2GRAY) except: continue image_blur = cv2.GaussianBlur(image_gray, (1, 1), 0) # thresh, image_thresh = cv2.threshold(image_blur, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) thresh, image_thresh = cv2.threshold(image_blur, 150, 255, cv2.THRESH_BINARY) # print(pytesseract.image_to_string(image_thresh, config='-c tessedit_char_whitelist=0123456789 -psm 7')) # detect text in the cropped image after preprocessing is done text = pytesseract.image_to_string(image_thresh, config='-psm 7') cv2.putText(frame_original, text, (endX, endY), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), lineType=cv2.LINE_AA) # show the frame with bounding boxes drawn cv2.imshow("Text Detection", frame_original) out.write(frame_original) # if q is pressed, quit key = cv2.waitKey(1) & 0xFF if key == ord("q"): break fps.stop() print("Elapsed Time: ", fps.elapsed()) print("FPS: ", fps.fps()) # stop the webcam and destroy all windows #vs.stop() vs.release() out.release() cv2.destroyAllWindows()
"Evaluating: ", progressbar.Percentage(), " ", progressbar.Bar(), " ", progressbar.ETA() ] pbar = progressbar.ProgressBar(maxval=len(queryIDs), widgets=widgets).start() # loop over the images for (i, queryID) in enumerate(sorted(queryIDs)): # lookup the relevant results for the query image queryRelevant = relevant[queryID] # load the query image and process it p = "{}/{}".format(args["dataset"], queryID) queryImage = cv2.imread(p) queryImage = imutils.resize(queryImage, width=320) queryImage = cv2.cvtColor(queryImage, cv2.COLOR_BGR2GRAY) # extract features from the query image and construct a bag-of-visual-words # from it (kps, descs) = dad.describe(queryImage) hist = bovw.describe(descs).tocoo() # perform the search and then spatially verify sr = searcher.search(hist, numResults=20) sv = spatialVerifier.rerank(kps, descs, sr, numResults=4) # compute the total number of relevant images in the top-4 results results = set([r[1] for r in sv.results[:4]]) inter = results.intersection(queryRelevant)
def find_template_in_image(main_image, template, resouce_name): templateCanny = cv2.Canny(template, 50, 200) (tH, tW) = templateCanny.shape[:2] # cv2.imshow("Template", template) # cv2.waitKey(0) gray_image = cv2.cvtColor(main_image, cv2.COLOR_BGR2GRAY) found = None # loop over the scales of the image for scale in np.linspace(0.2, 1.0, 20)[::-1]: # resize the image according to the scale, and keep track # of the ratio of the resizing resized = imutils.resize(gray_image, width=int(gray_image.shape[1] * scale)) r = gray_image.shape[1] / float(resized.shape[1]) # if the resized image is smaller than the template, then break # from the loop if resized.shape[0] < tH or resized.shape[1] < tW: break # detect edges in the resized, grayscale image and apply template # matching to find the template in the image edged = cv2.Canny(resized, 50, 200) result = cv2.matchTemplate(edged, templateCanny, cv2.TM_CCOEFF_NORMED) (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result) # check to see if the iteration should be visualized # if args.get("visualize", False): # draw a bounding box around the detected region # clone = np.dstack([edged, edged, edged]) # cv2.rectangle(clone, (maxLoc[0], maxLoc[1]), # (maxLoc[0] + tW, maxLoc[1] + tH), (0, 0, 255), 2) # cv2.imshow("Visualize", clone) # cv2.waitKey(0) # if we have found a new maximum correlation value, then update # the bookkeeping variable if found is None or maxVal > found[0]: found = (maxVal, maxLoc, r, resized) if not found: return False, None # unpack the bookkeeping variable and compute the (x, y) coordinates # of the bounding box based on the resized ratio (maxVal, maxLoc, r, resized) = found if find_template_in_image_many(resized, template): raise ResourceDuplication( 'More then one resource of type "{}"'.format(resouce_name)) threshold = 0.9 if maxVal <= threshold: return False, None (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r)) (endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r)) # cv2.rectangle(main_image, (startX, startY), (endX, endY), (0, 0, 255), 2) # cv2.imshow("Image", main_image) # cv2.waitKey(0) # find amount area # heightAmountRect = int((endY - startY) * 0.20) heightAmountRect = 38 (startX, startY) = (startX, endY + 2) endY = startY + int(heightAmountRect) # draw a bounding box around the detected result and display the image # cv2.rectangle(main_image, (startX, startY), (endX, endY), (0, 0, 255), 2) # cv2.imshow("Image", main_image) # cv2.waitKey(0) # cv2.imshow("Image", main_image[startY:endY, startX:endX]) # cv2.waitKey(0) # number = get_number(main_image[startY:endY, startX:endX]) number = NumberParser.get_number(main_image[startY:endY, startX:endX]) return found, number
from imutils.video import VideoStream import datetime import imutils import time import cv2 # vs = VideoStream(usePiCamera=1).start() vs = VideoStream(usePiCamera=0).start() time.sleep(1.0) from CameraStream import Server server = Server(Server.INPUT.OPENCV) while True: frame = vs.read() frame = imutils.resize(frame, width=320) timestamp = datetime.datetime.now() ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p") cv2.putText(frame, ts, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1) server.imshow("Frame", frame) vs.stop()
def age_gender(vs,dur): totalFrames = 0 totalDown = 0 totalUp = 0 rects=[] agen = [] gend = [] writer = None W= None H= None up = [] down = [] skipFrames = rate status = "" net = cv2.dnn.readNet("deploy.prototxt", "deploy.caffemodel") age_net = cv2.dnn.readNetFromCaffe('deploy_age.prototxt', 'age_net.caffemodel') gender_net = cv2.dnn.readNetFromCaffe('deploy_gender.prototxt', 'gender_net.caffemodel') font = cv2.FONT_HERSHEY_SIMPLEX MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746) age_list = ['(0, 2)', '(4, 6)', '(8, 12)', '(15, 20)', '(25, 32)', '(38, 43)', '(48, 53)', '(60, 100)'] gender_list = ['Male', 'Female'] CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] frameST = st.empty() trackers = [] trackableObjects = {} ct = CentroidTracker(maxDisappeared=40, maxDistance=50) widgets = ['Loading: ', progressbar.AnimatedMarker()] bar = progressbar.ProgressBar(widgets=widgets).start() with st.spinner('Processing...'): while True: frame = vs.read() frame = frame[1] if frame is None: break frame = imutils.resize(frame, width=320) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if W is None or H is None: (H, W) = frame.shape[:2] status = "Waiting" rects = [] if totalFrames % skipFrames < skipFrames/5: status = "Detecting" trackers = [] face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 5) for (x, y, w, h )in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 0), 2) face_img = frame[y:y+h, h:h+w].copy() blob = cv2.dnn.blobFromImage(face_img, 1, (227, 227), MODEL_MEAN_VALUES, swapRB=False) gender_net.setInput(blob) gender_preds = gender_net.forward() gender = gender_list[gender_preds[0].argmax()] print("Gender : " + gender) age_net.setInput(blob) age_preds = age_net.forward() age = age_list[age_preds[0].argmax()] print("Age Range: " + age) overlay_text = "%s %s" % (gender, age) cv2.putText(frame, overlay_text, (x, y), font, 1, (255, 255, 255), 2, cv2.LINE_AA) agen.append(age) gend.append(gender) blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5) net.setInput(blob) detections = net.forward() for i in np.arange(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > confidence_threshold: idx = int(detections[0, 0, i, 1]) if CLASSES[idx] not in ("person"): continue box = detections[0, 0, i, 3:7] * np.array([W, H, W, H]) (startX, startY, endX, endY) = box.astype("int") tracker = dlib.correlation_tracker() rect = dlib.rectangle(startX, startY, endX, endY) tracker.start_track(rgb, rect) trackers.append(tracker) else: for tracker in trackers: status = "Tracking" tracker.update(rgb) pos = tracker.get_position() startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) rects.append((startX, startY, endX, endY)) cv2.line(frame, (0, 0), (W, 0), (0, 255, 255), 2) objects = ct.update(rects) for (objectID, centroid) in objects.items(): to = trackableObjects.get(objectID, None) if to is None: to = TrackableObject(objectID, centroid) else: y = [c[1] for c in to.centroids] direction = centroid[1] - np.mean(y) to.centroids.append(centroid) if not to.counted: if direction < 0 and centroid[1] < H // 2: totalUp += 1 to.counted = True elif direction > 0 and centroid[1] > H // 2: totalDown += 1 to.counted = True up.append(totalUp) down.append(totalDown) trackableObjects[objectID] = to text = "ID {}".format(objectID) cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.circle(frame, (centroid[0], centroid[1]), 4, (0, 255, 0), -1) info = [("Up", totalUp),("Down", totalDown),("Status", status),] for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, H - ((i * 20) + 20)),cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) totalFrames += 1 frameST.image(frame, channels="BGR",caption='output video', use_column_width=True) if writer is None: fourcc = cv2.VideoWriter_fourcc(*"XVID") writer = cv2.VideoWriter("output.avi", fourcc, rate,(frame.shape[1], frame.shape[0]), True) return up, down, gend, agen
def main_loop(): #Check where this is running if running_on_jetson_nano(): vs = cv2.VideoCapture(get_jetson_gstreamer_source(), cv2.CAP_GSTREAMER) else: print("Nope") #Main loop while True: #get stream image ret, frame = vs.read() #if the image is empty break and re-try if frame is None: print("Nope 2") break frame = imutils.resize(frame, width=600, height=400) # resize the image blurred = cv2.GaussianBlur(frame, (9, 9), 0) # apply a Gaussian blur hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) # convert to HSV color space kernel = np.ones((2, 2), np.uint8) mask = cv2.inRange(hsv, lower, upper) # Selecting color from image based on bounds diliated = cv2.dilate(mask, kernel, iterations=2) # erroded = cv2.erode(diliated, kernel, iterations=1) # Erode the image # Find the contours on the mask and find the current center of the ball contours = cv2.findContours(erroded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(contours) center = None if len(contours) > 0: c = max(contours, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / (M["m00"] + .05)), int(M["m01"] / (M["m00"] + .05))) # print("Ball found at " + center) if radius > 10: cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) cv2.imshow("Ball", frame) cv2.imshow("Mask", mask) cv2.imshow("Erroded", erroded) cv2.imshow("Dialated", diliated) cv2.imshow("blurr", blurred) key = cv2.waitKey(1) & 0xFF if key == ord("q"): print("Quit") break vs.stop() vs.release() cv2.destroyAllWindows() return
l_h = cv2.getTrackbarPos("L - H", "Trackbars") l_s = cv2.getTrackbarPos("L - S", "Trackbars") l_v = cv2.getTrackbarPos("L - V", "Trackbars") u_h = cv2.getTrackbarPos("U - H", "Trackbars") u_s = cv2.getTrackbarPos("U - S", "Trackbars") u_v = cv2.getTrackbarPos("U - V", "Trackbars") lower_blue = np.array([l_h, l_s, l_v]) upper_blue = np.array([u_h, u_s, u_v]) mask = cv2.inRange(hsv, lower_blue, upper_blue) result = cv2.bitwise_and(image, image, mask=mask) # resize image for display purpose only resized_img = imutils.resize(image, width=image.shape[1] // 3, inter=3) resized_result = imutils.resize(result, width=image.shape[1] // 3, inter=3) cv2.imshow("Color Segmentation", np.hstack([resized_img, resized_result])) # perform labelling labels = measure.label(mask, neighbors=8, background=0) mask = np.zeros(mask.shape, dtype="uint8") for label in np.unique(labels): # if this is the background label, ignore it if label == 0:
args = vars(ap.parse_args()) # initialize the image descriptor and results montage desc = HSVDescriptor((4, 6, 3)) montage = ResultsMontage((240, 320), 5, 20) relevant = json.loads(open(args["relevant"]).read()) # load the relevant queries dictionary and look up the relevant results # for the query image queryFilename = args["query"][args["query"].rfind("/") + 1:] queryRelevant = relevant[queryFilename] # load the query image, display it and describe it print("[INFO] describing query...") query = cv2.imread(args["query"]) cv2.imshow("Query", imutils.resize(query, width=320)) cv2.waitKey(0) features = desc.describe(query) # perform the search print("[INFO] searching...") searcher = Searcher(args["index"]) results = searcher.search(features, numResults=20) # loop over the results for i, (score, resultID) in enumerate(results): # load the results and display it print(f"[INFO] {i+1}. {resultID} - {score:.2f}") result = cv2.imread(f"{args['dataset']}/{resultID}") montage.addResult(result, text=f"#{i+1}",
def gen_emp_face(emp_name): vs = cv2.VideoCapture(0) time.sleep(3.0) face_found = False cropped_face = None correct_image = None time_prev = time.time() count = 0 if os.path.exists(FACES_PARENT + emp_name): shutil.rmtree(FACES_PARENT + emp_name) os.mkdir(FACES_PARENT + emp_name) while True: __, frame = vs.read() frame = imutils.resize(frame, width=400) frame = cv2.flip(frame, 1) frame_orig = frame.copy() draw_map(frame, count) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = detector(gray, 0) if len(rects) == 0: text = "No face found" cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) elif len(rects) > 1: text = "{} face(s) found. Please be single in the frame.".format( len(rects)) cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) elif len(rects) == 1: text = "Face detected" cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) rect = rects[0] (bx, by, bw, bh) = face_utils.rect_to_bb(rect) cv2.rectangle(frame, (bx, by), (bx + bw, by + bh), (0, 255, 0), 1) shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) face_points = [] inside = True for (i, (x, y)) in enumerate(shape): cv2.circle(frame, (x, y), 1, (0, 0, 255), -1) face_points.append((x, y)) if x > 270 or x < 110 or y < 30 or y > 135 and i != 4: inside = False eye1_cen = ((face_points[0][0] + face_points[1][0]) // 2, (face_points[0][1] + face_points[1][1]) // 2) eye2_cen = ((face_points[2][0] + face_points[3][0]) // 2, (face_points[2][1] + face_points[3][1]) // 2) angle = slope(eye1_cen, eye2_cen) cv2.putText(frame, str(angle), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # print(time_prev-time.time()) if time.time() - time_prev > 1 and count <= 25 and inside: time_capture = time.time() time_prev = time_capture adjusted = correct_orientation(frame_orig, rect) cv2.imwrite( FACES_PARENT + emp_name + "/" + str(count) + ".jpg", adjusted) count += 1 elif not inside: cv2.putText(frame, "Be inside the grid", (150, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) if count > 25: cv2.putText(frame, "Done, Press q", (200, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break if count < 25: return False vs.release() cv2.destroyAllWindows() return True
import numpy as np import imutils import cv2 from matplotlib import pyplot as plt import pytesseract imagePath = "../../resources/img/national-id-card3.jpg" ori_img = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE) ori_img = imutils.resize(ori_img, height=1024) img = ori_img[350:900, 420:1300] img = cv2.GaussianBlur(img, (5, 5), 0) otsu_threshold_img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] kernel = np.ones((2, 2), np.uint8) otsu_threshold_img = cv2.dilate(otsu_threshold_img, kernel, iterations=2) otsu_threshold_img = cv2.erode(otsu_threshold_img, kernel, iterations=2) plt.imshow(otsu_threshold_img, cmap='gray', interpolation='bicubic') plt.show() print( pytesseract.image_to_string(otsu_threshold_img, lang='ben+eng', config="--tessdata-dir ../tessdata --psm 3"))
ap.add_argument("-p", "--shape-predictor", required=True, help="path to facial landmark predictor") ap.add_argument("-i", "--image", required=True, help="path to input image") args = vars(ap.parse_args()) # initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor and the face aligner detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(args["shape_predictor"]) fa = FaceAligner(predictor, desiredFaceWidth=256) # load the input image, resize it, and convert it to grayscale image = cv2.imread(args["image"]) image = imutils.resize(image, width=800) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # show the original input image and detect faces in the grayscale # image cv2.imshow("Input", image) rects = detector(gray, 2) # start = time.time() # end = time.time() # loop over the face detections for rect in rects: # extract the ROI of the *original* face, then align the face # using facial landmarks (x, y, w, h) = rect_to_bb(rect) faceOrig = imutils.resize(image[y:y + h, x:x + w], width=256)
my_file = "/home/pcroot/Desktop/ABODA-master/video11.avi" my_file_path = Path(my_file) if not my_file_path.is_file(): print("Video does not exist") exit() stream = cv2.VideoCapture(my_file) # stream = cv2.VideoCapture(0) fps = FPS().start() first_run = True (ret, frame) = stream.read() while not ret: (ret, frame) = stream.read() frame = imutils.resize(frame, width=450) adjusted = adjust_gamma(frame, gamma=gamma) # gamma correction frame = adjusted (height, width, channel) = frame.shape image_shape = (height, width) rgb = IntensityProcessing(image_shape) bbox_last_frame_proposals = [] static_objects = [] count = 0 n_frame = 0 while True: # fps._numFrames < 120 (ret, frame) = stream.read() if not ret: break
greenUpper = (80, 255, 255) blueLower = (110, 50, 50) blueUpper = (130, 255, 255) pts = deque( maxlen=args["buffer"]) #stores the set of points the object moves on. new = deque(maxlen=args["buffer"]) camera = cv2.VideoCapture(0) while True: (grabbed, frame) = camera.read() #take the available frame frame = imutils.resize(frame, width=600) # reduce the frame size to increase FPS. hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # construct masks for the colors , one mask per color. mask = cv2.inRange(hsv, greenLower, greenUpper) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) mask2 = cv2.inRange(hsv, blueLower, blueUpper) mask2 = cv2.erode(mask2, None, iterations=2) mask2 = cv2.dilate(mask2, None, iterations=2) # find contours in the masks and initialize the current (x, y) center of the ball cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None
def open_video(): cap = cv.VideoCapture(0) global data_to_print data_to_print = "" pts = collections.deque() def nothing(x): pass def safe_div(x, y): if y == 0: return 0 return x / y barsWindow = 'Bars' hl = 'Hue Low' hh = 'Hue High' sl = 'Saturation Low' sh = 'Saturation High' vl = 'Value Low' vh = 'Value High' cv.namedWindow(barsWindow, flags=cv.WINDOW_AUTOSIZE) cv.createTrackbar(hl, barsWindow, 0, 179, nothing) cv.createTrackbar(hh, barsWindow, 0, 179, nothing) cv.createTrackbar(sl, barsWindow, 0, 255, nothing) cv.createTrackbar(sh, barsWindow, 0, 255, nothing) cv.createTrackbar(vl, barsWindow, 0, 255, nothing) cv.createTrackbar(vh, barsWindow, 0, 255, nothing) cv.setTrackbarPos(hl, barsWindow, 0) cv.setTrackbarPos(hh, barsWindow, 179) cv.setTrackbarPos(sl, barsWindow, 0) cv.setTrackbarPos(sh, barsWindow, 255) cv.setTrackbarPos(vl, barsWindow, 0) cv.setTrackbarPos(vh, barsWindow, 255) while True: x, frame = cap.read() if cv.waitKey(5) & 0xFF == ord('q'): break frame = imutils.resize(frame, width=600) hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) hul = cv.getTrackbarPos(hl, barsWindow) huh = cv.getTrackbarPos(hh, barsWindow) sal = cv.getTrackbarPos(sl, barsWindow) sah = cv.getTrackbarPos(sh, barsWindow) val = cv.getTrackbarPos(vl, barsWindow) vah = cv.getTrackbarPos(vh, barsWindow) colorLower = np.array([hul, sal, val]) colorUpper = np.array([huh, sah, vah]) #colorLower = (24, 100, 100) #colorUpper = (44, 255, 255) mask = cv.inRange(hsv, colorLower, colorUpper) mask = cv.erode(mask, None, iterations=2) mask = cv.dilate(mask, None, iterations=2) cnts = cv.findContours(mask.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)[-2] print(cnts) if len(cnts) > 0: c = max(cnts, key=cv.contourArea) rect = cv.minAreaRect(c) box = cv.boxPoints(rect) box = np.int0(box) (tl, tr, br, bl) = box def midpoint(ptA, ptB): return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5) (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) pixelsPerMetric = 1 dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric cv.putText(frame, "{:.1f}mm".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv.putText(frame, "{:.1f}mm".format(dimB), (int(trbrX + 10), int(trbrY)), cv.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) global length global breadth length = "{:.1f}mm".format(dimA) breadth = "{:.1f}mm".format(dimB) M = cv.moments(c) print(M) cX = int(safe_div(M["m10"], M["m00"])) cY = int(safe_div(M["m01"], M["m00"])) global area area = "{:.1f}mm".format(M["m00"]) pts.appendleft((cX, cY)) for i in range(1, len(pts)): if pts[i - 1] is None or pts[i] is None: continue cv.line(frame, pts[i - 1], pts[i], (0, 0, 255), 3) cv.drawContours(frame, [box], 0, (123, 200, 255), 2) cv.circle(frame, (cX, cY), 5, (255, 255, 255), -1) cv.imshow('frame', frame)
IMAGE_PATHS = ["images/Picture1.jpg", "images/Picture2.jpg", "images/Picture3.jpg"] # load the furst image that contains an object that is KNOWN TO BE 2 feet # from our camera, then find the paper marker in the image, and initialize # the focal length #讀入第一張圖,通過已知距離計算相機焦距QQ image = cv2.imread(IMAGE_PATHS[0]) marker = find_marker(image) focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH while(1): # get a frame ret, frame = cap.read() frame = imutils.resize(frame, width=min(400, frame.shape[1])) # detect people in the image (rects, weights) = hog.detectMultiScale(frame, winStride=(4, 4), padding=(8, 8), scale=1.05) rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) # 非極大抑制 消除多餘的框 找到最佳人體 pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) # 畫出邊框 for (xA, yA, xB, yB) in pick: cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2) # show a frame
# Left eye left corner 37 (-225.0, 170.0, -135.0), # Right eye right corne 46 (225.0, 170.0, -135.0), (-150.0, -150.0, -125.0), # Left Mouth corner 49 # Right mouth corner 55 (150.0, -150.0, -125.0) ]) while True: # grab the frame from the threaded video stream, resize it to # have a maximum width of 400 pixels, and convert it to # grayscale frame = vs.read() frame = imutils.resize(frame, width=1024, height=576) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) size = gray.shape # detect faces in the grayscale frame rects = detector(gray, 0) # check to see if a face was detected, and if so, draw the total # number of faces on the frame if len(rects) > 0: text = "{} face(s) found".format(len(rects)) cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # loop over the face detections for rect in rects:
import cv2 import sys import imutils img_color = cv2.imread("./half_dark.jpg", cv2.IMREAD_COLOR) if img_color is None: print("Failed") sys.exit(1) img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY) img_binary = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5 , 4) img_gray = imutils.resize(img_gray, height = 500, width = 500) img_binary = imutils.resize(img_binary, height = 500, width = 500) while True: cv2.imshow("GrayScale", img_gray) cv2.imshow("Binary", img_binary) if(cv2.waitKey(1) & 0xFF == 27): break cv2.destroyAllWindows()
# Parse our args args = vars(ap.parse_args()) # Adjust the filepath to our new temp file # Consoliadte the raw arguments to a string, ignoring the width rawArgs = "" for x in range(1, len(sys.argv)): if sys.argv[x] == "-w" or sys.argv[x] == "--width": break if sys.argv[x] == "-i" or sys.argv[x] == "--image": sys.argv[x + 1] = "temp.png" rawArgs += sys.argv[x] + " " # Resize our image with the given width, assuming the width is not more than twice the default width image = cv2.imread(args["image"]) h, w = image.shape[:2] if int(args["width"]) < (w * 2): w = int(args["width"]) resized = imutils.resize(image, width=w) # Write the resized image to a temp file cv2.imwrite('temp.png', resized) # Format and call our command for detect_shapes command = "python detect_shapes.py {}".format(rawArgs) os.system(command) # Delete the temp image os.remove('temp.png')