cv.PutText(img, barcode_format, (10, top), font, (254, 142, 20)) top += increase cv.PutText(img, barcode_value, (10, top), font, (254, 142, 20)) top += increase cv.PutText(img, "************************", (10, top), font, (254, 142, 20)) top += increase # Display Webcam preview cv.ShowImage(title, img) else: img = cv.QueryFrame(capture) cv.ShowImage(title, img) key_code = cv.WaitKey(10) if key_code != -1: print(key_code) if key_code == 27: # 27 => ESC break elif key_code == 13: # 13 => Enter if is_camera_paused: is_camera_paused = False else: is_camera_paused = True is_saved = False cv.DestroyAllWindows()
#Compute movement curr_points, status, err = cv.CalcOpticalFlowPyrLK( prev_gray, gray, prevPyr, currPyr, prev_points, (10, 10), 3, (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03), 0) #If points status are ok and distance not negligible keep the point k = 0 for i in range(len(curr_points)): nb = abs(int(prev_points[i][0]) - int(curr_points[i][0])) + abs( int(prev_points[i][1]) - int(curr_points[i][1])) if status[i] and nb > 2: initial[k] = initial[i] curr_points[k] = curr_points[i] k += 1 curr_points = curr_points[:k] initial = initial[:k] for i in range(len(curr_points)): cv.Line(output, (int(initial[i][0]), int(initial[i][1])), (int(curr_points[i][0]), int(curr_points[i][1])), (255, 255, 255)) cv.Circle(output, (int(curr_points[i][0]), int(curr_points[i][1])), 3, (255, 255, 255)) cv.Copy(gray, prev_gray) prev_points = curr_points cv.ShowImage("The Video", output) c = cv.WaitKey(1) if c == 27: #Esc on Windows break
if __name__ == "__main__": udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) split = 30 splittedstr = [""] * split cv.NamedWindow("ClientCAM", 1) capture = cv.CaptureFromCAM(0) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 480) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 360) while True: img = cv.QueryFrame(capture) jpgstring = cv.EncodeImage(".jpeg", img).tostring() # cv.ShowImage("ClientCAM", img) #jpgstring = zlib.compress(jpgstring) jpglen = len(jpgstring) for i in range(split - 1): splittedstr[i] = jpgstring[jpglen / split * i:jpglen / split * (i + 1)] splittedstr[split - 1] = jpgstring[jpglen / split * (split - 1):] for i in range(split): udp.sendto(splittedstr[i], ("127.0.0.1", 12345)) if cv.WaitKey(10) == 27: break cv.DestroyAllWindows() udp.close()
def run(self): started = time.time() menu = np.zeros((500, 600, 3), np.uint8) menu[5:492, 7:592] = (0, 100, 220) cv2.putText(menu, "MENU", (195, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2) cv2.putText(menu, "_____", (190, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (70, 70, 70), 2) cv2.putText(menu, "Button 0 : Number", (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) cv2.putText(menu, " 1: One , 2: Two , 3: Three", (10, 120), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) cv2.putText(menu, " 4: Four , 5: Five", (10, 140), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) cv2.putText(menu, "Button 1 : OS Commands", (10, 170), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) cv2.putText(menu, " 1: Play Music , 2: Open Web , 3: Kill Processes", (10, 190), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) cv2.putText(menu, " 4: Open Text Files , 5: Open Image Files", (10, 210), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) cv2.putText(menu, "Button 2 : Sentences", (10, 240), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) cv2.putText(menu, " 1: Hey , 2: How r u ? , 3: I am Fine !", (10, 260), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) cv2.putText(menu, " 4: See u , 5: Bye", (10, 280), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) cv2.putText(menu, "Button 3 : Animations", (10, 310), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) cv2.putText( menu, " 1: Horizontal Motion , 2: Vertical Motion , 3: Diagonal Motion", (10, 330), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) cv2.putText(menu, " 4: Fast Motion , 5: Slow Motion", (10, 350), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) #cv2.putText(menu, "Button 4 : Arduino", (10, 380), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) #cv2.putText(menu, " 1: LED On , 2: LED Off , 3: Obstacle Detection", (10, 400), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) #cv2.putText(menu, " 4: Not Defined , 5: Not Defined", (10, 420), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1) cv2.imshow('Menu', menu) cv2.moveWindow('Menu', 0, 0) c_one = 1 c_two = 1 c_three = 1 c_four = 1 c_zero = 1 c_check = 7 c_button = 0 c_reac_time = 0 path = 'E:\project_main\shanta' files = os.listdir(path) coun = 0 print "Gesture Recognizer Started :\n Button 0" process = ('notepad.exe', 'WINWORD.EXE', 'IEXPLORE.exe', 'ois.exe', 'dllhost.exe', 'excel.exe', 'mpc-hc.exe', 'wmplayer.exe', 'wordpad.exe', 'calc.exe', 'powerpnt.exe', 'firefox.exe') fle = open('abc.txt', 'w') print >> fle, "----------------------------------OUTPUT OF THE GESTURES-----------------------------------" print >> fle, "---Instructions---" print >> fle, "Button 0 : Number" print >> fle, "Button 1 : OS Commands" print >> fle, "Button 2 : Sentences" print >> fle, "Button 3 : Animations" #print >> fle, "Button 4 : Arduino" print >> fle, "--------------------------------------------------------------------------------------------" print >> fle, "-------------------------------------------OUTPUT-------------------------------------------" print >> fle, "Button No. 0" str = "" while True: ####### ####### ####### ret, img = cap.read() cv2.rectangle(img, (400, 400), (100, 100), (0, 255, 0), 0) crop_img = img[100:400, 100:400] grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY) value = (35, 35) blurred = cv2.GaussianBlur(grey, value, 0) _, thresh1 = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) #cv2.imshow('Thresholded', thresh1) contours, hierarchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cnt = max(contours, key=lambda x: cv2.contourArea(x)) x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(crop_img, (x, y), (x + w, y + h), (0, 0, 255), 0) hull = cv2.convexHull(cnt) #drawing = np.zeros(crop_img.shape, np.uint8) #cv2.drawContours(drawing, [cnt], 0, (0, 255, 0), 0) #cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 0) hull = cv2.convexHull(cnt, returnPoints=False) count_defects = 0 cv2.drawContours(thresh1, contours, -1, (0, 255, 0), 3) defects = cv2.convexityDefects(cnt, hull) for i in range(cv2.convexityDefects(cnt, hull).shape[0]): s, e, f, d = defects[i, 0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2) b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2) c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2) angle = math.acos((b**2 + c**2 - a**2) / (2 * b * c)) * 57 if angle <= 90: count_defects += 1 cv2.circle(crop_img, far, 1, [0, 0, 255], -1) # dist = cv2.pointPolygonTest(cnt,far,True) cv2.line(crop_img, start, end, [0, 255, 0], 2) # cv2.circle(crop_img,far,5,[0,0,255],-1) c_reac_time = c_reac_time + 1 if count_defects == 1: cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) c_one = c_one + 1 c_two = c_three = c_four = c_zero = 1 if (c_one == c_check): if (c_button == 0): str = "two" cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) elif (c_button == 1): wb.open("www.rknec.edu") str = "Opened web" elif (c_button == 2): str = "How r u ?" elif (c_button == 3): tk = Tk() xspeed = 0 yspeed = 3 canvas = Canvas(tk, width=WIDTHH, height=HEIGHTT) tk.title("BALL ANIMATION") canvas.pack() coun = 1 ball = canvas.create_oval(10, 10, 60, 60, fill="blue") while True: try: coun = coun + 1 if (coun == 500): tk.destroy() break canvas.move(ball, xspeed, yspeed) pos = canvas.coords(ball) if pos[3] >= HEIGHTT or pos[1] <= 0: yspeed = -yspeed # if pos[2] >= WIDTHH or pos[0] <= 0: # xspeed = -xspeed tk.update() time.sleep(0.01) except Exception as e: break str = "Played Vertical Animation" coun = 0 cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) speaker.Speak(str) print >> fle, str elif count_defects == 2: cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) c_two = c_two + 1 c_three = c_one = c_four = c_zero = 1 if (c_two == c_check): if (c_button == 0): str = "Three" cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 2) elif (c_button == 1): str = "Killed No Processes" for i in process: if (pc.processExists(i)): os.system("taskkill /f /im " + i) str = "Killed Process : " + i cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 1) elif (c_button == 2): str = "i m fine" elif (c_button == 3): tk = Tk() xspeed = 3 yspeed = 3 canvas = Canvas(tk, width=WIDTHH, height=HEIGHTT) tk.title("BALL ANIMATION") canvas.pack() coun = 1 ball = canvas.create_oval(10, 10, 60, 60, fill="blue") while True: try: coun = coun + 1 if (coun == 500): tk.destroy() break canvas.move(ball, xspeed, yspeed) pos = canvas.coords(ball) if pos[3] >= HEIGHTT or pos[1] <= 0: yspeed = -yspeed if pos[2] >= WIDTHH or pos[0] <= 0: xspeed = -xspeed tk.update() time.sleep(0.01) except Exception as e: break str = "Played Diagonal Animation" coun = 0 cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 1) speaker.Speak(str) print >> fle, str elif count_defects == 3: cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) c_three = c_three + 1 c_two = c_one = c_four = c_zero = 1 if (c_three == c_check): if (c_button == 0): str = "Four" cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) elif (c_button == 1): files_txt = [i for i in files if i.endswith('.txt')] for i in files_txt: os.startfile(path + "\\" + i) str = "Opened text files" elif (c_button == 2): str = "see u" elif (c_button == 3): tk = Tk() yspeed = 7 xspeed = 7 canvas = Canvas(tk, width=WIDTHH, height=HEIGHTT) tk.title("BALL ANIMATION") canvas.pack() coun = 1 ball = canvas.create_oval(10, 10, 60, 60, fill="blue") while True: try: coun = coun + 1 if (coun == 500): tk.destroy() break canvas.move(ball, xspeed, yspeed) pos = canvas.coords(ball) if pos[3] >= HEIGHTT or pos[1] <= 0: yspeed = -yspeed if pos[2] >= WIDTHH or pos[0] <= 0: xspeed = -xspeed tk.update() time.sleep(0.01) except Exception as e: break str = "Played Fast Animation" coun = 0 cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) speaker.Speak(str) print >> fle, str elif count_defects == 4: cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) c_four = c_four + 1 c_two = c_three = c_one = c_zero = 1 if (c_four == c_check): if (c_button == 0): str = "Five" cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) elif (c_button == 1): for i in files: if i.endswith('.JPG'): imgs = PIL.Image.open(path + "\\" + i) imgs.show() str = "Opened image files" elif (c_button == 2): str = "bye" elif (c_button == 3): tk = Tk() yspeed = 1 xspeed = 1 canvas = Canvas(tk, width=WIDTHH, height=HEIGHTT) tk.title("BALL ANIMATION") canvas.pack() coun = 1 ball = canvas.create_oval(10, 10, 60, 60, fill="blue") while True: try: coun = coun + 1 if (coun == 500): tk.destroy() break canvas.move(ball, xspeed, yspeed) pos = canvas.coords(ball) if pos[3] >= HEIGHTT or pos[1] <= 0: yspeed = -yspeed if pos[2] >= WIDTHH or pos[0] <= 0: xspeed = -xspeed tk.update() time.sleep(0.01) except Exception as e: break str = "Played Slow Animation" coun = 0 cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) speaker.Speak(str) print >> fle, str elif count_defects == 0: cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) c_zero = c_zero + 1 c_two = c_three = c_four = c_one = 1 if (c_zero == c_check): if (c_button == 0): str = "one" cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) elif (c_button == 1): os.system('start ratta.mp3') str = "Playing music" elif (c_button == 2): str = "Hey" elif (c_button == 3): tk = Tk() yspeed = 0 xspeed = 5 canvas = Canvas(tk, width=WIDTHH, height=HEIGHTT) tk.title("BALL ANIMATION") canvas.pack() coun = 1 ball = canvas.create_oval(10, 10, 60, 60, fill="blue") while True: try: coun = coun + 1 if (coun == 500): tk.destroy() break canvas.move(ball, xspeed, yspeed) pos = canvas.coords(ball) # if pos[3] >= HEIGHTT or pos[1] <= 0: # yspeed = -yspeed if pos[2] >= WIDTHH or pos[0] <= 0: xspeed = -xspeed tk.update() time.sleep(0.01) except Exception as e: break str = "Played Horizontal Animation" coun = 0 cv2.putText(img, str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 1) speaker.Speak(str) print >> fle, str img[1:100, 540:640] = (0, 0, 255) cv2.imshow('Gesture', img) cv2.moveWindow('Gesture', 650, 0) #all_img = np.hstack((drawing, crop_img)) #cv2.imshow('Contours', all_img) ##### ##### ##### curframe1 = cv.QueryFrame(self.capture) curframe = curframe1[1:100, 540:640] instant = time.time() # Get timestamp o the frame self.processImage(curframe) # Process the image # if not self.isRecording: if self.somethingHasMoved(): self.trigger_time = instant # Update the trigger_time if instant > started + 5: # Wait 5 second after the webcam start for luminosity adjusting etc.. if c_reac_time >= 10: c_button = np.mod(c_button + 1, 4) c_reac_time = 0 print datetime.now().strftime( "%b %d, %H:%M:%S"), "Button No. ", c_button print >> fle, datetime.now().strftime( "%b %d, %H:%M:%S"), "Button No. ", c_button cv.Copy(self.frame2gray, self.frame1gray) c = cv.WaitKey(1) % 0x100 if c == 27: break
if __name__ == "__main__": cv.NamedWindow("win") if len(sys.argv) > 1: filename = sys.argv[1] im = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_GRAYSCALE) im3 = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_COLOR) else: try: # try opening local copy of image fileName = '../cpp/left01.jpg' im = cv.LoadImageM(fileName, False) im3 = cv.LoadImageM(fileName, True) except: # if local copy cannot be opened, try downloading it url = 'https://raw.github.com/Itseez/opencv/master/samples/cpp/left01.jpg' filedata = urllib2.urlopen(url).read() imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) cv.SetData(imagefiledata, filedata, len(filedata)) im = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_GRAYSCALE) im3 = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) chessboard_dim = (9, 6) found_all, corners = cv.FindChessboardCorners(im, chessboard_dim) print found_all, len(corners) cv.DrawChessboardCorners(im3, chessboard_dim, corners, found_all) cv.ShowImage("win", im3) cv.WaitKey() cv.DestroyAllWindows()
def runtracking(): global rgb_image, hsv_image, hsvmouse, pausecam, hsvgreen, hsvyellow, hsvblue, hsvred, homographycomputed global hsvyellowtab, hsvrange global homography, pose_flag global hsvyellowmin, hsvyellowmax, hsvgreenmin, hsvgreenmax, hsvbluemin, hsvbluemax, hsvredmin, hsvredmax global cycloppoint, righteyepoint, lefteyepoint global capture, pausecam, size_image global yellowmask_image, greenmask_image, redmask_image, bluemask_image global nb_pts, modelepoints, blob_centers global rx, ry, rz global background size_thumb = [size_image[0] / 2, size_image[1] / 2] thumbgreen = cv.CreateImage(size_thumb, cv.IPL_DEPTH_8U, 1) thumbred = cv.CreateImage(size_thumb, cv.IPL_DEPTH_8U, 1) thumbblue = cv.CreateImage(size_thumb, cv.IPL_DEPTH_8U, 1) thumbyellow = cv.CreateImage(size_thumb, cv.IPL_DEPTH_8U, 1) cv.NamedWindow("GreenBlobDetection", cv.CV_WINDOW_AUTOSIZE) cv.ShowImage("GreenBlobDetection", thumbgreen) cv.NamedWindow("YellowBlobDetection", cv.CV_WINDOW_AUTOSIZE) cv.ShowImage("YellowBlobDetection", thumbyellow) cv.NamedWindow("BlueBlobDetection", cv.CV_WINDOW_AUTOSIZE) cv.ShowImage("BlueBlobDetection", thumbblue) cv.NamedWindow("RedBlobDetection", cv.CV_WINDOW_AUTOSIZE) cv.ShowImage("RedBlobDetection", thumbred) rgb_image = cv.QueryFrame(capture) cv.NamedWindow("Source", cv.CV_WINDOW_AUTOSIZE) cv.ShowImage("Source", rgb_image) cv.SetMouseCallback("Source", getObjectHSV) print "Hit ESC key to quit..." #infinite loop for processing while True: time.sleep(0.02) blobcentergreen = findBlob(rgb_image, hsv_image, greenmask_image, greenblob_image, hsvrange, hsvgreenmin, hsvgreenmax) blobcenteryellow = findBlob(rgb_image, hsv_image, yellowmask_image, yellowblob_image, hsvrange, hsvyellowmin, hsvyellowmax) blobcenterblue = findBlob(rgb_image, hsv_image, bluemask_image, blueblob_image, hsvrange, hsvbluemin, hsvbluemax) blobcenterred = findBlob(rgb_image, hsv_image, redmask_image, redblob_image, hsvrange, hsvredmin, hsvredmax) if not pausecam: if (blobcentergreen != None): cv.Resize(greenblob_image, thumbgreen) #cv.ShowImage("GreenBlobDetection", greenblob_image) cv.ShowImage("GreenBlobDetection", thumbgreen) #print "green center: %d %d %d" %blobcentergreen if (blobcenteryellow != None): cv.Resize(yellowblob_image, thumbyellow) cv.ShowImage("YellowBlobDetection", thumbyellow) #print "yellow center: %d %d %d" %blobcenteryellow if (blobcenterblue != None): cv.Resize(blueblob_image, thumbblue) cv.ShowImage("BlueBlobDetection", thumbblue) #print "blue center: %d %d %d" %blobcenterblue if (blobcenterred != None): cv.Resize(redblob_image, thumbred) cv.ShowImage("RedBlobDetection", thumbred) #print "red center: %d %d %d" %blobcenterred cv.ShowImage("Source", rgb_image) c = cv.WaitKey(7) % 0x100 if c == 27: break if c == ord('p') or c == ord('P'): pausecam = not pausecam if c == ord('y'): hsvyellowtab.append(hsvmouse) hsvyellowmin = mintab(hsvyellowtab) hsvyellowmax = maxtab(hsvyellowtab) print "minyellow" print hsvyellowmin print "maxyellow" print hsvyellowmax if c == ord('Y'): if (len(hsvyellowtab) > 0): hsvyellowtab.pop(len(hsvyellowtab) - 1) if (len(hsvyellowtab) != 0): hsvyellowmin = mintab(hsvyellowtab) hsvyellowmax = maxtab(hsvyellowtab) else: hsvyellowmin = [255, 255, 255] hsvyellowmax = [0, 0, 0] if c == ord('g'): hsvgreentab.append(hsvmouse) hsvgreenmin = mintab(hsvgreentab) hsvgreenmax = maxtab(hsvgreentab) print "mingreen" print hsvgreenmin print "maxgreen" print hsvgreenmax if c == ord('G'): if (len(hsvgreentab) > 0): hsvgreentab.pop(len(hsvgreentab) - 1) if (len(hsvgreentab) != 0): hsvgreenmin = mintab(hsvgreentab) hsvgreenmax = maxtab(hsvgreentab) else: hsvgreenmin = [255, 255, 255] hsvgreenmax = [0, 0, 0] if c == ord('r'): hsvredtab.append(hsvmouse) hsvredmin = mintab(hsvredtab) hsvredmax = maxtab(hsvredtab) print "minred" print hsvredmin print "maxred" print hsvredmax if c == ord('R'): if (len(hsvredtab) > 0): hsvredtab.pop(len(hsvredtab) - 1) if (len(hsvredtab) != 0): hsvredmin = mintab(hsvredtab) hsvredmax = maxtab(hsvredtab) else: hsvredmin = [255, 255, 255] hsvredmax = [0, 0, 0] if c == ord('b'): hsvbluetab.append(hsvmouse) hsvbluemin = mintab(hsvbluetab) hsvbluemax = maxtab(hsvbluetab) print "minblue" print hsvbluemin print "maxblue" print hsvbluemax if c == ord('B'): if (len(hsvbluetab) > 0): hsvbluetab.pop(len(hsvbluetab) - 1) if (len(hsvbluetab) != 0): hsvbluemin = mintab(hsvbluetab) hsvbluemax = maxtab(hsvbluetab) else: hsvbluemin = [255, 255, 255] hsvbluemax = [0, 0, 0] #if c == ord('R') : # step=0 if not pausecam: rgb_image = cv.QueryFrame(capture) #after blob center detection we need to launch pose estimation if ((blobcentergreen != None) and (blobcenteryellow != None) and (blobcenterblue != None) and (blobcenterred != None)): #order is Yellow,blue,red, green pose_flag = 1 blob_centers = [] blob_centers.append((blobcenteryellow[0] - size_image[0] / 2, blobcenteryellow[1] - size_image[1] / 2)) blob_centers.append((blobcenterblue[0] - size_image[0] / 2, blobcenterblue[1] - size_image[1] / 2)) blob_centers.append((blobcenterred[0] - size_image[0] / 2, blobcenterred[1] - size_image[1] / 2)) blob_centers.append((blobcentergreen[0] - size_image[0] / 2, blobcentergreen[1] - size_image[1] / 2)) #get the tracking matrix (orientation and position) result with POSIT method in the tracker (camera) referential matrix = find_pose(nb_pts, blob_centers, modelepoints) #We want to get the tracking result in the world referencial, i.e. with at 60 cm of the midle of the screen, with Y up, and Z behind you. #The tracker referential in the camera referential, with the X axis pointing to the #left, the Y axis pointing down, and the Z axis pointing behind you, and with the camera as origin. #We thus pre multiply to have the traking results in the world referential, and not in the tracker (camera) referential. (pre-product) pre_tranform_matrix = WordToTrackerTransform(matrix) #We do not want to track the center of the body referential (the right up point of the glasses), but the midlle of the two eyes in monoscopic (cyclops eye), #or left and right eyes in stereoscopic. #We thus post multiply the world traking results in the world referential, using the referential of the eye in the body referential (glasses) pre_tranform_matrix_post_cylcope_eye = BodyToCyclopsEyeTransform( pre_tranform_matrix) poscyclope = [ pre_tranform_matrix_post_cylcope_eye[3][0], pre_tranform_matrix_post_cylcope_eye[3][1], pre_tranform_matrix_post_cylcope_eye[3][2] ] print "poscylope", poscyclope pre_tranform_matrix_post_left_eye = BodyToLeftEyeTransform( pre_tranform_matrix) posleft = [ pre_tranform_matrix_post_left_eye[3][0], pre_tranform_matrix_post_left_eye[3][1], pre_tranform_matrix_post_left_eye[3][2] ] #print "posleft",posleft pre_tranform_matrix_post_right_eye = BodyToRightEyeTransform( pre_tranform_matrix) posright = [ pre_tranform_matrix_post_right_eye[3][0], pre_tranform_matrix_post_right_eye[3][1], pre_tranform_matrix_post_right_eye[3][2] ] #print "posright",posright sendPosition("/tracker/head/pos_xyz/cyclope_eye", poscyclope) sendPosition("/tracker/head/pos_xyz/left_eye", posleft) sendPosition("/tracker/head/pos_xyz/right_eye", posright) else: print "Traking failed"
def tangent(self, dx, dy, Mask=None, method="cv"): '''This function calculates the gradient orientation of each pixel that is in Mask''' tangent = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_8U, 1) divSize = cv.GetSize(dx) tangent16U = cv.CreateImage(divSize, cv.IPL_DEPTH_32F, 1) cv.SetZero(tangent16U) if method == "slow": for x in range(divSize[0]): for y in range(divSize[1]): if Mask == None: tang = math.atan2(dy[y, x], dx[y, x]) * self.constant tangent16U[y, x] = int(tang) elif Mask[y, x] > 0: tang = math.atan2(dy[y, x], dx[y, x]) * self.constant tangent16U[y, x] = int(tang) elif Mask[y, x] == 0: tangent16U[y, x] = 0 elif method == "cv": #Calculated the arctan2 which give -pi to pi range #I create numpy arrays then reshape them in to 1 Dimesnion. #Next, I calculated arctan2 and change the range to 0-2pi and make it in degrees #I reshape back to picture format and return the picture #Numpy formatting (width, height) = cv.GetSize(dx) matdx = cv.CreateMat(height, width, cv.CV_16SC1) matdy = cv.CreateMat(height, width, cv.CV_16SC1) cv.SetZero(matdx) cv.SetZero(matdy) cv.Copy(dx, matdx, Mask) cv.Copy(dy, matdy, Mask) a = numpy.asarray(matdx) b = numpy.asarray(matdy) #Reshaping to one dimension ar = numpy.reshape(a, a.size) br = numpy.reshape(b, b.size) #Calculating Arc Tangent with quadrant information c = numpy.arctan2(br, ar) #Turning it to -180 to 180 range z = numpy.multiply(c, self.constant) result = z.astype(numpy.int32) result[result < 0] += 360 tang = numpy.reshape(result, (height, width)) tang = tang.astype(numpy.float32) mat = cv.fromarray(tang) cv.Copy(mat, tangent16U) else: dxTemp = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_16S, 1) dyTemp = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_16S, 1) zero = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_16S, 1) cv.Add(zero, dx, dxTemp, Mask) cv.Add(zero, dy, dyTemp, Mask) dx = dxTemp dy = dyTemp for x in range(divSize[0]): for y in range(divSize[1]): if Mask[y, x] == 0: tangent16U[y, x] = 0 continue tang = math.atan2(dy[y, x], dx[y, x]) * self.constant tangent16U[y, x] = int(tang) if self.visualize: #tangent2 = cv.CreateImage(cv.GetSize(dy), cv.CV_16SC1, 1) cv.ConvertScaleAbs(tangent16U, tangent) cv.EqualizeHist(tangent, tangent) while True: cv.NamedWindow("Tangent") cv.ShowImage("Tangent", tangent) c = cv.WaitKey(5) if c > 0: break cv.DestroyAllWindows() return tangent16U
features += cv.FindCornerSubPix( grey, [pt], (win_size, win_size), (-1, -1), (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) # we are no longer in "add_remove_pt" mode add_remove_pt = False # swapping prev_grey, grey = grey, prev_grey prev_pyramid, pyramid = pyramid, prev_pyramid need_to_init = False # we can now display the image cv.ShowImage('LkDemo', image) # handle events c = cv.WaitKey(10) % 0x100 if c == 27: # user has press the ESC key, so exit break # processing depending on the character if 32 <= c and c < 128: cc = chr(c).lower() if cc == 'r': need_to_init = True elif cc == 'c': features = [] elif cc == 'n': night_mode = not night_mode elif cc == ' ':
def main(): cap = cv.CaptureFromCAM(0) cv.NamedWindow("camera", cv.CV_WINDOW_NORMAL) cv.SetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH, 720) cv.SetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT, 540) cols = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH)) rows = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT)) grey = cv.CreateImage((cols, rows), 8, 1) cumulated = cv.CreateImage((cols, rows), 8, 1) equalize = True laplace = False settings = { "canny_avg": 10, } threshold1 = 600 threshold2 = 200 settings_names = sorted(settings.keys()) setting_current = 0 setting_name = settings_names[setting_current] while True: im = cv.QueryFrame(cap) cv.CvtColor(im, grey, cv.CV_BGR2GRAY) if equalize: cv.Smooth(grey, grey, param1=5, param2=5) cv.EqualizeHist(grey, grey) if laplace: cv.Canny(grey, grey, threshold1, threshold2) avg = cv.Avg(cumulated)[0] if avg > settings["canny_avg"] * 1.2: threshold1 *= 1.1 threshold2 = threshold1 / 2.5 if avg < settings["canny_avg"] / 1.2: threshold1 /= 1.1 threshold2 = threshold1 / 2.5 cv.ShowImage("camera", grey) key = cv.WaitKey(1) if key not in (-1, 1114085, 1245157): # None, block print("Key %d" % key) if key in ( # Capture one frame 1048675, # c 99, # c ): filenames = save_image(cap, 1) print("Capturing: %s" % ", ".join(list(filenames))) if key in ( # Capture ten frames 1114179, # C 1179715, # C (block) 65603, # C 131139, # C (block) ): filenames = save_image(cap, 10) print("Capturing: %s" % ", ".join(list(filenames))) elif key in ( # Toggle equalization 1114181, # e 1048677, # E 1179717, # E (block) 1245285, # e (block) 101, # e 65605, # E 131141, # E (block) 196709, # e (block) ): equalize = not equalize print("Equalize: %s" % equalize) elif key in ( # Toggle laplace 1179724, # l 1048684, # L (block( 1114188, # L 108, 65612, 131148, 196716, ): laplace = not laplace print("Laplace: %s" % laplace) elif key in ( # Increment value 1113938, # Up 65362, ): settings[setting_name] += 1 print("%s := %d" % (setting_name, settings[setting_name])) elif key in ( # Decrement value 1113940, # Down 65364, ): settings[setting_name] -= 1 print("%s := %d" % (setting_name, settings[setting_name])) elif key in ( # Next setting 1113939, # Right 65363, ): setting_current = (setting_current + 1) % len(settings_names) setting_name = settings_names[setting_current] print("%s : %d" % (setting_name, settings[setting_name])) elif key in ( # Prev setting 1113937, # Left 65361, ): setting_current = (setting_current - 1) % len(settings_names) setting_name = settings_names[setting_current] print("%s : %d" % (setting_name, settings[setting_name])) elif key in ( # Exit 27, # ESC 1048603, # ESC 1114193, # q 1048689, # Q 1179729, # Q (block) 1245297, # q (block) 113, 65617, 131153, 196721, ): break
def image_callback(self, data): # Store the image header in a global variable self.image_header = data.header # Time this loop to get cycles per second start = rospy.get_time() # Convert the ROS image to OpenCV format using a cv_bridge helper function frame = self.convert_image(data) # Some webcams invert the image if self.flip_image: frame = cv2.flip(frame, 0) # Store the frame width and height in a pair of global variables if self.frame_width is None: self.frame_size = (frame.shape[1], frame.shape[0]) self.frame_width, self.frame_height = self.frame_size # Create the marker image we will use for display purposes if self.marker_image is None: self.marker_image = np.zeros_like(frame) # Copy the current frame to the global image in case we need it elsewhere self.frame = frame.copy() # Process the image to detect and track objects or features processed_image = self.process_image(frame) # Make a global copy self.processed_image = processed_image.copy() # Merge the processed image and the marker image self.display_image = cv2.bitwise_or(self.processed_image, self.marker_image) # Handle keyboard events self.keystroke = cv.WaitKey(250) # Compute the time for this loop and estimate CPS as a running average end = rospy.get_time() duration = end - start fps = int(1.0 / duration) self.cps_values.append(fps) if len(self.cps_values) > self.cps_n_values: self.cps_values.pop(0) self.cps = int(sum(self.cps_values) / len(self.cps_values)) # Display CPS and image resolution if asked to if self.show_text: font_face = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 """ Print cycles per second (CPS) and resolution (RES) at top of the image """ if self.frame_size[0] >= 640: vstart = 25 voffset = int(50 + self.frame_size[1] / 120.) elif self.frame_size[0] == 320: vstart = 15 voffset = int(35 + self.frame_size[1] / 120.) else: vstart = 10 voffset = int(20 + self.frame_size[1] / 120.) cv2.putText(self.display_image, "CPS: " + str(self.cps), (10, vstart), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText( self.display_image, "RES: " + str(self.frame_size[0]) + "X" + str(self.frame_size[1]), (10, voffset), font_face, font_scale, cv.RGB(255, 255, 0)) # Process any keyboard commands self.keystroke = cv2.waitKey(250) if self.keystroke != -1: try: cc = chr(self.keystroke & 255).lower() if cc == 't': self.show_text = not self.show_text elif cc == ' ' or 's': cv2.imwrite( self.output_folder + "/image_" + str(self.image_count) + ".jpg", self.processed_image) rospy.loginfo("Saving image_" + str(self.image_count) + " to " + self.output_folder) self.image_count += 1 elif cc == 'q' or '\x1b': # The has press the q key, so exit rospy.signal_shutdown("User hit q key to quit.") except: pass # Publish the display image self.publish_display_image(self.display_image) # Update the image display cv2.imshow(self.node_name, self.display_image)
print "Moving to" , sensor_value steppera.changeSpeed(int(100 * sign(int(float(sensor_value)) - 0)),abs(int(float(sensor_value)) - 0)) #turnAStep = int(float(sensor_value)) #print turnAStep while (steppera.IsTurning() == True): time.sleep(0.2) if capture: frame_copy = None while True: #cv.WaitKey(1000) frame = cv.QueryFrame(capture) if not frame: print "No Fraeme found - press a key to exit" cv.WaitKey(0) break if not frame_copy: frame_copy = cv.CreateImage((frame.width,frame.height), cv.IPL_DEPTH_8U, frame.nChannels) # frame_copy = cv.CreateImage((frame.width,frame.height), # cv.IPL_DEPTH_8U, frame.nChannels) if frame.origin == cv.IPL_ORIGIN_TL: cv.Copy(frame, frame_copy) else: cv.Flip(frame, frame_copy, 0) print "detect and draw" detect_and_draw(frame_copy, cascade)
# plot points def draw_cross(center, color, d): cv.Line(img, (center[0] - d, center[1] - d), (center[0] + d, center[1] + d), color, 1, cv.CV_AA, 0) cv.Line(img, (center[0] + d, center[1] - d), (center[0] - d, center[1] + d), color, 1, cv.CV_AA, 0) cv.Zero(img) draw_cross(state_pt, cv.CV_RGB(255, 255, 255), 3) draw_cross(measurement_pt, cv.CV_RGB(255, 0,0), 3) draw_cross(predict_pt, cv.CV_RGB(0, 255, 0), 3) cv.Line(img, state_pt, measurement_pt, cv.CV_RGB(255, 0,0), 3, cv. CV_AA, 0) cv.Line(img, state_pt, predict_pt, cv.CV_RGB(255, 255, 0), 3, cv. CV_AA, 0) cv.KalmanCorrect(kalman, measurement) cv.RandArr(rng, process_noise, cv.CV_RAND_NORMAL, cv.RealScalar(0), cv.RealScalar(sqrt(kalman.process_noise_cov[0, 0]))) cv.MatMulAdd(kalman.transition_matrix, state, process_noise, state) cv.ShowImage("Kalman", img) code = cv.WaitKey(100) % 0x100 if code != -1: break if code in [27, ord('q'), ord('Q')]: break cv.DestroyWindow("Kalman")
def snapL(self, L): for i, img in enumerate(L): cv.NamedWindow("snap-%d" % i, 1) cv.ShowImage("snap-%d" % i, img) cv.WaitKey() cv.DestroyAllWindows()
def divide(self, image, column=8, row=8, option="normal", overlap=0): #Checks whether inputs are correct if self.image_check(image) < 0: return -1 if self.mn_check(column, row) < 0: return -2 if option == "normal": #sizeTuple is a tuple as follow: (width, height) sizeTuple = cv.GetSize(image) widthSlice = math.floor(sizeTuple[0] / column) heightSlice = math.floor(sizeTuple[1] / row) widthReminder = sizeTuple[0] % column heightReminder = sizeTuple[1] % column List = [] rowIndex = 0 columnIndex = 0 #In these two while loops we make every sub image, crop it from original image and add it to a list #if there are reminders, the width and height of the final row/columns will differ while columnIndex < sizeTuple[0]: if columnIndex == sizeTuple[0] - widthSlice - widthReminder: width = widthSlice + widthReminder else: width = widthSlice while rowIndex < sizeTuple[1]: if rowIndex == sizeTuple[1] - heightSlice - heightReminder: height = heightSlice + heightReminder else: height = heightSlice if columnIndex - overlap <= 0: #Overlapping the rectangles. column = columnIndex else: column = columnIndex - overlap if rowIndex - overlap <= 0: row = rowIndex else: row = rowIndex - overlap subRect = (column, row, width, height) List.append(self.crop(image, subRect)) rowIndex += height rowIndex = 0 columnIndex += width if (self.visualize): windows = range(len(List)) for x in windows: cv.NamedWindow(str(x)) while True: for x in windows: cv.ShowImage(str(x), List[x]) c = cv.WaitKey(5) if c > 0: print c break cv.DestroyAllWindows() return List else: #sizeTuple is a tuple as follow: (width, height) columnstep = 1 rowstep = 1 maxcolumn = column maxrow = row List = [] while columnstep <= maxcolumn and rowstep <= maxrow: column = columnstep row = rowstep sizeTuple = cv.GetSize(image) widthSlice = math.floor(sizeTuple[0] / column) heightSlice = math.floor(sizeTuple[1] / row) widthReminder = sizeTuple[0] % column heightReminder = sizeTuple[1] % column rowIndex = 0 columnIndex = 0 #In these two while loops we make every sub image, crop it from original image and add it to a list #if there are reminders, the width and height of the final row/columns will differ while columnIndex < sizeTuple[0]: if columnIndex == sizeTuple[0] - widthSlice - widthReminder: width = widthSlice + widthReminder else: width = widthSlice while rowIndex < sizeTuple[1]: if rowIndex == sizeTuple[ 1] - heightSlice - heightReminder: height = heightSlice + heightReminder else: height = heightSlice if columnIndex - overlap <= 0: #Overlapping the rectangles. column = columnIndex else: column = columnIndex - overlap if rowIndex - overlap <= 0: row = rowIndex else: row = rowIndex - overlap subRect = (int(column), int(row), int(width), int(height)) List.append(self.crop(image, subRect)) rowIndex += height rowIndex = 0 columnIndex += width if (self.visualize): windows = range(len(List)) for x in windows: cv.NamedWindow(str(x)) while True: for x in windows: tan = cv.CreateImage(cv.GetSize(List[x]), cv.IPL_DEPTH_8U, 1) final = cv.CreateImage(cv.GetSize(List[x]), cv.IPL_DEPTH_8U, 1) cv.CvtColor(List[x], tan, cv.CV_RGB2GRAY) cv.EqualizeHist(tan, final) cv.NamedWindow(str(x)) cv.ShowImage(str(x), final) c = cv.WaitKey(5) if c > 0: print c break cv.DestroyAllWindows() columnstep *= 2 rowstep *= 2 return List
def drawrectangle(): cv.Rectangle(img, (50, 50), (100, 100), (0, 0, 0), 4, 8) display(img, "Destination") cv.WaitKey(0)
pad_w, pad_h = int(0.15*w), int(0.05*h) cv2.rectangle(img, (x+pad_w, y+pad_h), (x+w-pad_w, y+h-pad_h), (0, 255, 0), thickness) while True: #format = IplImage frame = cv.QueryFrame(cap) #converts to numpy array arr = np.asarray(frame[:,:]) hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) #takes a numpy array as input found, w = hog.detectMultiScale(arr, winStride=(16,16), padding=(32,32), scale=1.05) print found if len(found) != 0: for f in found: draw_detections(arr, f) #converts numpt array back to IplImage cv.ShowImage("PeopleFinder", cv.fromarray(arr)) if cv.WaitKey(1) & 0xFF == ord('q'): break cap.release() cv.DestroyAllWindows()
def drawcircle(): cv.Circle(img, (100, 100), 20, (0, 0, 0), 4, 8) display(img, "Destination") cv.WaitKey(0)
Target = cv.CloneImage(Img) while True: # image processing if Dilate: cv.Dilate(Target, Target, iterations=Dilate) Dilate = 0 if Erode: cv.Erode(Target, Target, iterations=Erode) Erode = 0 if Threshold: cv.Threshold(Img, Target, Threshold_Min, 0xff, cv.CV_THRESH_BINARY) cv.And(Target, Mask, Target) show_image() # keystroke processing k = cv.WaitKey(0) print k if k > 66000: continue if k < 256: k = chr(k) else: if k > 65506 and k != 65535: k -= 65506 k = chr(k - 30) if k == 65288 and Edit_x >= 0: # BS print 'deleting column' Grid_Points_x.remove(Grid_Points_x[Edit_x]) Edit_x = -1 Grid_Entries_x -= 1
def image_callback(self, data): # Store the image header in a global variable self.image_header = data.header # Time this loop to get cycles per second start = time.time() # Convert the ROS image to OpenCV format using a cv_bridge helper function frame = self.convert_image(data) # Some webcams invert the image if self.flip_image: frame = cv2.flip(frame, 0) # Store the frame width and height in a pair of global variables if self.frame_width is None: self.frame_size = (frame.shape[1], frame.shape[0]) self.frame_width, self.frame_height = self.frame_size # Create the marker image we will use for display purposes if self.marker_image is None: self.marker_image = np.zeros_like(frame) # Copy the current frame to the global image in case we need it elsewhere self.frame = frame.copy() # Reset the marker image if we're not displaying the history if not self.keep_marker_history: self.marker_image = np.zeros_like(self.marker_image) # Process the image to detect and track objects or features processed_image = self.process_image(frame) # If the result is a greyscale image, convert to 3-channel for display purposes """ #if processed_image.channels == 1: #cv.CvtColor(processed_image, self.processed_image, cv.CV_GRAY2BGR) #else: # Make a global copy self.processed_image = processed_image.copy() # Display the user-selection rectangle or point self.display_selection() # Night mode: only display the markers if self.night_mode: self.processed_image = np.zeros_like(self.processed_image) # Merge the processed image and the marker image self.display_image = cv2.bitwise_or(self.processed_image, self.marker_image) # If we have a track box, then display it. The track box can be either a regular # cvRect (x,y,w,h) or a rotated Rect (center, size, angle). if self.show_boxes: if self.track_box is not None and self.is_rect_nonzero( self.track_box): if len(self.track_box) == 4: x, y, w, h = self.track_box size = (w, h) center = (x + w / 2, y + h / 2) angle = 0 self.track_box = (center, size, angle) else: (center, size, angle) = self.track_box # For face tracking, an upright rectangle looks best if self.face_tracking: pt1 = (int(center[0] - size[0] / 2), int(center[1] - size[1] / 2)) pt2 = (int(center[0] + size[0] / 2), int(center[1] + size[1] / 2)) cv2.rectangle(self.display_image, pt1, pt2, cv.RGB(50, 255, 50), self.feature_size, 8, 0) else: # Otherwise, display a rotated rectangle vertices = np.int0(cv2.cv.BoxPoints(self.track_box)) cv2.drawContours(self.display_image, [vertices], 0, cv.RGB(50, 255, 50), self.feature_size) # If we don't yet have a track box, display the detect box if present elif self.detect_box is not None and self.is_rect_nonzero( self.detect_box): (pt1_x, pt1_y, w, h) = self.detect_box if self.show_boxes: cv2.rectangle(self.display_image, (pt1_x, pt1_y), (pt1_x + w, pt1_y + h), cv.RGB(50, 255, 50), self.feature_size, 8, 0) # Publish the ROI self.publish_roi() # Handle keyboard events self.keystroke = cv.WaitKey(5) # Compute the time for this loop and estimate CPS as a running average end = time.time() duration = end - start fps = int(1.0 / duration) self.cps_values.append(fps) if len(self.cps_values) > self.cps_n_values: self.cps_values.pop(0) self.cps = int(sum(self.cps_values) / len(self.cps_values)) # Display CPS and image resolution if asked to if self.show_text: font_face = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 """ Print cycles per second (CPS) and resolution (RES) at top of the image """ if self.frame_size[0] >= 640: vstart = 25 voffset = int(50 + self.frame_size[1] / 120.) elif self.frame_size[0] == 320: vstart = 15 voffset = int(35 + self.frame_size[1] / 120.) else: vstart = 10 voffset = int(20 + self.frame_size[1] / 120.) cv2.putText(self.display_image, "CPS: " + str(self.cps), (10, vstart), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText( self.display_image, "RES: " + str(self.frame_size[0]) + "X" + str(self.frame_size[1]), (10, voffset), font_face, font_scale, cv.RGB(255, 255, 0)) # Update the image display cv2.imshow(self.node_name, self.display_image) # Process any keyboard commands self.keystroke = cv2.waitKey(5) if self.keystroke != -1: try: cc = chr(self.keystroke & 255).lower() if cc == 'n': self.night_mode = not self.night_mode elif cc == 'f': self.show_features = not self.show_features elif cc == 'b': self.show_boxes = not self.show_boxes elif cc == 't': self.show_text = not self.show_text elif cc == 'q': # The has press the q key, so exit rospy.signal_shutdown("User hit q key to quit.") except: pass
def __init__(self): rospy.init_node('video2ros', anonymous=False) rospy.on_shutdown(self.cleanup) """ Define the input (path to video file) as a ROS parameter so it can be defined in a launch file or on the command line """ self.input = rospy.get_param("~input", "") """ Define the image publisher with generic topic name "output" so that it can be remapped in the launch file. """ image_pub = rospy.Publisher("output", Image, queue_size=5) # The target frames per second for the video self.fps = rospy.get_param("~fps", 25) # Do we restart the video when the end is reached? self.loop = rospy.get_param("~loop", False) # Start the video paused? self.start_paused = rospy.get_param("~start_paused", False) # Show text feedback by default? self.show_text = rospy.get_param("~show_text", True) # Resize the original video? self.resize_video = rospy.get_param("~resize_video", False) # If self.resize_video is True, set the desired width and height here self.resize_width = rospy.get_param("~resize_width", 0) self.resize_height = rospy.get_param("~resize_height", 0) # Initialize a few variables self.paused = self.start_paused self.loop_video = True self.keystroke = None self.restart = False self.last_frame = None # Initialize the text font font_face = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 # Define the capture object as pointing to the input file self.capture = cv2.VideoCapture(self.input) # Get the original frames per second fps = self.capture.get(cv.CV_CAP_PROP_FPS) # Get the original frame size self.frame_size = (self.capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT), self.capture.get(cv.CV_CAP_PROP_FRAME_WIDTH)) # Check that we actually have a valid video source if fps == 0.0: print "Video source", self.input, "not found!" return None # Bring the fps up to the specified rate try: fps = int(fps * self.fps / fps) except: fps = self.fps # Create the display window cv.NamedWindow("Video Playback", True) # autosize the display cv.MoveWindow("Video Playback", 375, 25) # Create the CvBridge object bridge = CvBridge() # Enter the main processing loop while not rospy.is_shutdown(): # Get the next frame from the video frame = self.get_frame() # Convert the frame to ROS format try: image_pub.publish(bridge.cv2_to_imgmsg(frame, "bgr8")) except CvBridgeError, e: print e # Create a copy of the frame for displaying the text display_image = frame.copy() if self.show_text: cv2.putText(display_image, "FPS: " + str(self.fps), (10, 20), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, "Keyboard commands:", (20, int(self.frame_size[0] * 0.6)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, " ", (20, int(self.frame_size[0] * 0.65)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, "space - toggle pause/play", (20, int(self.frame_size[0] * 0.72)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, " r - restart video from beginning", (20, int(self.frame_size[0] * 0.79)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, " t - hide/show this text", (20, int(self.frame_size[0] * 0.86)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, " q - quit the program", (20, int(self.frame_size[0] * 0.93)), font_face, font_scale, cv.RGB(255, 255, 0)) # Merge the original image and the display image (text overlay) display_image = cv2.bitwise_or(frame, display_image) # Now display the image. cv2.imshow("Video Playback", display_image) """ Handle keyboard events """ self.keystroke = cv.WaitKey(1000 / fps) """ Process any keyboard commands """ if self.keystroke != -1: try: cc = chr(self.keystroke & 255).lower() if cc == 'q': """ user has press the q key, so exit """ rospy.signal_shutdown("User hit q key to quit.") elif cc == ' ': """ Pause or continue the video """ self.paused = not self.paused elif cc == 'r': """ Restart the video from the beginning """ self.restart = True elif cc == 't': """ Toggle display of text help message """ self.show_text = not self.show_text except: pass
def cannyTangent(self, image, smooth=True, T1=20, T2=250, eq=False, method="cv", method2="fast"): '''Gets the thresholded edge from opencv canny, and use the locations to take out the magnitude and gradient from manually calculated gradient using sobel mask''' gsimage = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1) smoothed = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1) final = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1) if image.channels > 1: temp = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1) cv.CvtColor(image, temp, cv.CV_BGR2GRAY) else: temp = image if eq: cv.EqualizeHist(temp, gsimage) else: cv.Copy(temp, gsimage) if smooth: cv.Smooth(gsimage, smoothed) final = smoothed else: cv.Copy(gsimage, final) tempo = self.visualize self.visualize = False #a = time.time() gradient = self.cannyGradient(final, t1=T1, t2=T2) #print "single canny time:", time.time() - a #a = time.time() (dx, dy) = self.sobelGradient(final) #print "single sobel time:", time.time() - a #a = time.time() tangent = self.tangent(dx, dy, gradient, method=method) #print "single tangent time:", time.time() - a #a = time.time() magnitude = self.Magnitude(dx, dy, gradient, method=method2) #print "single magnitude time:", time.time() - a self.visualize = tempo if self.visualize: jinjer = 0 while True: tan = cv.CreateImage(cv.GetSize(tangent), cv.IPL_DEPTH_8U, 1) timp = cv.CreateImage(cv.GetSize(tangent), cv.IPL_DEPTH_8U, 1) cv.Convert(tangent, tan) cv.EqualizeHist(tan, timp) cv.NamedWindow("Original") cv.MoveWindow("Original", 0, 0) cv.ShowImage("Original", final) #cv.NamedWindow("TangentCanny") #cv.ShowImage("TangentCanny", timp) cv.NamedWindow("magnitude") cv.MoveWindow("magnitude", 640, 0) cv.ShowImage("magnitude", magnitude) c = cv.WaitKey(5) jinjer += 1 if c > 0 or jinjer > 1000000: break cv.DestroyAllWindows() return (tangent, magnitude)
def simpleblurnoscale(src, dst): cv.Smooth(src, dst, cv.CV_BLUR, 9, 9) display(src, "Source Image") display(dst, "Simple No Scale Smoothing") cv.WaitKey(0) cv.DestroyAllWindows()
from kafka import KafkaClient, SimpleProducer, SimpleConsumer import numpy as np import cv2 from cv2 import cv kafka = KafkaClient("129.168.6.53:9092") consumer = SimpleConsumer(kafka, "python", "output1", max_buffer_size=9932768) for msg in consumer: print msg.offset, msg.message.value jsonobj = json.loads(msg.message.value) print jsonobj['data'] # pdata = base64.b64decode(jsonobj['data']) ## print pdata # #mat= cv.CreateMat(jsonobj['rows'], jsonobj['cols'], jsonobj['type']) # #cv.imshow("Image", mat) # nparr = np.fromstring(pdata, np.uint8) # img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) # img_ipl = cv.CreateImageHeader((img_np.shape[1], img_np.shape[0]), cv.IPL_DEPTH_8U, 3) # cv.SetData(img_ipl, img_np.tostring(), img_np.dtype.itemsize * 3 * img_np.shape[1]) # ## mat = cv.pImgMat(img_ipl,0); # filename="/home/zym/storm_out/"+jsonobj['cameraId']+jsonobj['timestamp']+".png" ## cv.imwrite(filename,mat) # cv.ShowImage("output", img_ipl) # cv.SaveImage(filename, img_ipl) cv.WaitKey(10) # if not msg.message.value.strip(): # break # time.sleep(2) kafka.close()
pwm = Adafruit_PCA9685.PCA9685() pwm.set_pwm_freq(60) GPIO.setmode(GPIO.BOARD) cv.NamedWindow("camera", 1) capture = cv2.VideoCapture(0) faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') while True: _, img = capture.read() img = cv2.resize(img, (320, 240)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=3, minSize=(30, 30), flags=cv.CV_HAAR_SCALE_IMAGE ) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) #pulse = 150~650 : 0 ~ 180deg pwm.set_pwm(0, 0, 400 - (x+w)/2) # h pwm.set_pwm(1, 0, 400 - (y+h)/2) # v cv2.imshow("camera", img) if cv.WaitKey(10) > 0: break cv.DestroyAllWindows()
def Dilation(pos): element = cv.CreateStructuringElementEx(pos * 2 + 1, pos * 2 + 1, pos, pos, element_shape) cv.Dilate(src, dest, element, 1) cv.ShowImage("Erosion & Dilation", dest) if __name__ == "__main__": if len(sys.argv) > 1: src = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_COLOR) else: url = 'https://raw.github.com/opencv/opencv/master/samples/c/fruits.jpg' filedata = urllib2.urlopen(url).read() imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) cv.SetData(imagefiledata, filedata, len(filedata)) src = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) image = cv.CloneImage(src) dest = cv.CloneImage(src) cv.NamedWindow("Opening & Closing", 1) cv.NamedWindow("Erosion & Dilation", 1) cv.ShowImage("Opening & Closing", src) cv.ShowImage("Erosion & Dilation", src) cv.CreateTrackbar("Open", "Opening & Closing", 0, 10, Opening) cv.CreateTrackbar("Close", "Opening & Closing", 0, 10, Closing) cv.CreateTrackbar("Dilate", "Erosion & Dilation", 0, 10, Dilation) cv.CreateTrackbar("Erode", "Erosion & Dilation", 0, 10, Erosion) cv.WaitKey(0) cv.DestroyWindow("Opening & Closing") cv.DestroyWindow("Erosion & Dilation")
def ellipsedraw(): cv.Ellipse(img, (50, 150), (40, 80), 100.0, 0.0, 360.0, (0, 0, 0), 8, 2) display(img, "Destination") cv.WaitKey(0)
else: print "We need a camera input! Specify camera index e.g. 0" sys.exit(0) cv.NamedWindow("result", 1) if capture: frame_copy = None move(panGpioPin, servoPanPosition) move(tiltGpioPin, servoTiltPosition) while True: frame = cv.QueryFrame(capture) if not frame: cv.WaitKey(0) break if not frame_copy: frame_copy = cv.CreateImage((frame.width, frame.height), cv.IPL_DEPTH_8U, frame.nChannels) if frame.origin == cv.IPL_ORIGIN_TL: cv.Copy(frame, frame_copy) else: cv.Flip(frame, frame_copy, 0) midScreenX = (frame.width / 2) midScreenY = (frame.height / 2) midFace = detect_and_draw(frame_copy, cascade) if midFace is not None:
def linedraw(): cv.Line(img, (0, 0), (100, 100), (0, 0, 0), 4, 8) display(img, "Destination") cv.WaitKey(0)
flag = 0 while True: #grabbing a frame, applying blur, flipping the image frame = cv.QueryFrame(camera) cv.Smooth(frame, frame, cv.CV_BLUR, 3) cv.Flip(frame, frame, 1) #drawing the rectangle and writing the text temp1 = cv.CloneImage(frame) cv.Rectangle(temp1, point1, point2, color, 1) cv.PutText(temp1, "Place in box", (430, 240), font, color) cv.PutText(temp1, "then hit q", (430, 260), font, color) #taking snapshot after q is pressed if cv.WaitKey(10) == 113: flag = 1 cv.SetImageROI(temp1, (300, 200, 100, 100)) template = cv.CloneImage(temp1) cv.ResetImageROI(temp1) cv.DestroyWindow("Image") if flag == 0: cv.NamedWindow("Image", 1) cv.MoveWindow("Image", 300, 0) cv.ShowImage("Image", temp1) continue W, H = cv.GetSize(frame) w, h = cv.GetSize(template)
def main(): cv.NamedWindow("camera", 1) #cv.NamedWindow("camera1", 1) #cv.NamedWindow("camera2", 1) capture = cv.CaptureFromCAM(0) mid = cv.QueryFrame(capture) frames = cv.QueryFrame(capture) #res = cv.CreateImage(cv.GetSize(frames),8,1); #cv.ShowImage("camera1", res) #cv.ShowImage("camera", img) res = cv.CreateMat(frames.height, frames.width, cv.CV_8U) while True: mid = cv.CloneImage(frames) img = cv.QueryFrame(capture) mid = differ(mid, img) cv.ShowImage("camera1", mid) """ horize = cv.CreateMat(mid.height, mid.width, cv.CV_8U) horize(mid,horize); verize= cv.CreateMat(mid.height, mid.width, cv.CV_8U) verize(mid,verize); commonize(mid,horize,verize) cv.ShowImage("camera2", mid) cv.CvtColor(mid, im_mid, cv.CV_RGB2GRAY) cv.ShowImage("camera1", im_mid) #print im_mid im_img = cv.CreateMat(img.height, img.width, cv.CV_8U) cv.CvtColor(img, im_img, cv.CV_RGB2GRAY) cv.AbsDiff(im_mid, im_img , res) cv.ShowImage("After AbsDiff", res) cv.Smooth(res, res, cv.CV_BLUR, 5,5) cv.ShowImage("Image1", res) element = cv.CreateStructuringElementEx(5*2+1, 5*2+1, 5, 5, cv.CV_SHAPE_RECT) cv.MorphologyEx(res, res, None, None, cv.CV_MOP_OPEN) cv.MorphologyEx(res, res, None, None, cv.CV_MOP_CLOSE) cv.ShowImage("Image2", res) cv.Threshold(res, res, 10, 255, cv.CV_THRESH_BINARY_INV) cv.ShowImage("Image", res) #im_mid = np.asarray(img) #im_img = np.asarray(img) #img_res = np.asarray(res) #cv2.absdiff(im_mid,im_img,img_res) #cv.ShowImage("camera", img_res) #differ(im_mid,im_img) #print im_mid """ if cv.WaitKey(10) == 27: break