Exemplo n.º 1
0
"""

import cv2
#import autopilot_agent

import cv
import autopilot_agent as aa

cam = cv2.VideoCapture(0) # Get the stream of your webcam
running = True
while running:    # get current frame of video    
    running, frame = cam.read()    
    if running:    
		# cv2.rectangle(frame, (x1, y1), (x2, y2), (255,0,0), 2)   
        # print frame.shape # == 480,640
        size = frame.shape
        act = aa.action(frame,size[1],size[0],0,0,0,0,0,0,0,0,0)
        c = (int(act[1]*500),int(act[3]*500))
        print size[1]/2,size[0]/2
        cv2.line(frame,(size[1]/2,size[0]/2),((size[1]/2)+c[0],(size[0]/2)+c[1]),(255,0,0),2)        
        coord_hist = (act[1],act[3])
#        print (zap, phi, theta, gaz, yaw)

        cv2.imshow('frame', frame)        
        if cv2.waitKey(1) & 0xFF == 27:             # escape key pressed            
            running = False    
        else:        # error reading frame        
            print 'error reading video feed'
cam.release()
cv2.destroyAllWindows()
    def run_old(self):
        while self.running:
            # This should be an numpy image array
            pixelarray = self.drone.get_image()  # get an frame form the Drone
            if pixelarray is not None:  # check whether the frame is not empty
                self.frame = pixelarray[:, :, ::-1].copy()  #convert to a frame


#            ret, self.frame = self.cam.read()

            size = self.frame.shape
            act = aa.action(self.frame, size[1], size[0], 0, 0, 0, 0, 0, 0, 0,
                            0, 0)
            c = (int(act[5]), int(-act[6]))
            # print size[1]/2,size[0]/2
            cv2.line(self.frame, (size[1] / 2, size[0] / 2),
                     ((size[1] / 2) + c[0], (size[0] / 2) + c[1]), (255, 0, 0),
                     2)
            #           coord_hist = (act[1],act[3])

            if act[7] is not None:  # no face found
                self.steer_to(act[5], act[6], act[7])

            vis = self.frame.copy()
            hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))

            if self.selection:
                x0, y0, x1, y1 = self.selection
                self.track_window = (x0, y0, x1 - x0, y1 - y0)
                hsv_roi = hsv[y0:y1, x0:x1]
                mask_roi = mask[y0:y1, x0:x1]
                hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180])
                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                self.hist = hist.reshape(-1)
                self.show_hist()

                vis_roi = vis[y0:y1, x0:x1]
                cv2.bitwise_not(vis_roi, vis_roi)
                vis[mask == 0] = 0

            if self.tracking_state == 1:
                self.selection = None
                prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
                prob &= mask
                term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                             10, 1)
                for i, value in enumerate(self.track_window):
                    if value <= 0:
                        #print i,value
                        self.track_window = (0, 0, 1000, 1000)
                        break
                #print self.track_window
                track_box, self.track_window = cv2.CamShift(
                    prob, self.track_window, term_crit)

                if self.show_backproj:
                    vis[:] = prob[..., np.newaxis]
                try:
                    cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                except:
                    pass
                    # print track_box

            cv2.imshow(self.window_name, vis)

            ch = 0xFF & cv2.waitKey(50)
            if ch == 27:
                break
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
        cv2.destroyAllWindows()
        print "video loop stopped."
Exemplo n.º 3
0
    def run_old(self):
        while self.running:
            # This should be an numpy image array
            pixelarray = self.drone.get_image() # get an frame form the Drone
            if pixelarray is not None: # check whether the frame is not empty
                self.frame = pixelarray[:, :, ::-1].copy() #convert to a frame
#            ret, self.frame = self.cam.read()
                
            size = self.frame.shape
            act = aa.action(self.frame,size[1],size[0],0,0,0,0,0,0,0,0,0)
            c = (int(act[5]),int(-act[6]))
            # print size[1]/2,size[0]/2
            cv2.line(self.frame,(size[1]/2,size[0]/2),((size[1]/2)+c[0],(size[0]/2)+c[1]),(255,0,0),2)
 #           coord_hist = (act[1],act[3])

            if act[7] is not None:  # no face found
                self.steer_to(act[5], act[6], act[7])
            
            vis = self.frame.copy()
            hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.)))

            if self.selection:
                x0, y0, x1, y1 = self.selection
                self.track_window = (x0, y0, x1-x0, y1-y0)
                hsv_roi = hsv[y0:y1, x0:x1]
                mask_roi = mask[y0:y1, x0:x1]
                hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                self.hist = hist.reshape(-1)
                self.show_hist()

                vis_roi = vis[y0:y1, x0:x1]
                cv2.bitwise_not(vis_roi, vis_roi)
                vis[mask == 0] = 0

            if self.tracking_state == 1:
                self.selection = None
                prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
                prob &= mask
                term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
                for i,value in enumerate(self.track_window):
                    if value<=0:
                        #print i,value
                        self.track_window = (0,0,1000,1000)
                        break
                #print self.track_window
                track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit)
                
                if self.show_backproj:
                    vis[:] = prob[...,np.newaxis]
                try:
                    cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                except:
                    pass
                    # print track_box

            cv2.imshow(self.window_name, vis)

            ch = 0xFF & cv2.waitKey(50)
            if ch == 27:
                break
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
        cv2.destroyAllWindows()
        print "video loop stopped."