def calibrate(self):
     while not self.buffer_full:
         time.sleep(0.5)
     cv2.namedWindow("Calibration")
     cv2.startWindowThread()
     cv2.setMouseCallback("Calibration", self.mouse_press)
     self.calibration_index = 0
     self.board_center = [(0,0)]*BUFFER_SIZE
     self.fish_center = [(0,0)]*BUFFER_SIZE
     t_steps = []
     for img in self.calibration_buffer:
         self.calibration_image = img[1]
         t_steps.append(img[0])
         self.calibration_phase = 'board'
         cv2.imshow('Calibration', img[1])
         k = cv2.waitKey(0)
         self.calibration_index += 1
     cv2.destroyWindow('Calibration')
     # Now that we have some data let's use it
     x = np.mean([point.x for point in self.board_center])
     y = np.mean([point.y for point in self.board_center])
     
     self.center_point = Point(x,y)
     self._calibrate(self.center_point, self.fish_center, t_steps)
     self.calibrated = True
    def __init__(self):

        cv2.namedWindow("detected circles", 1)
        cv2.startWindowThread()
        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("/turtlebot_2/camera/rgb/image_raw",
                                          Image, self.callback)
Exemple #3
0
	def videoCapture(self):
		number = 0
		cv2.namedWindow("Drone %s" %self.numVehicle, cv2.cv.CV_WINDOW_NORMAL)
		cv2.cv.ResizeWindow("Drone %s" %self.numVehicle, 640, 480)
		cv2.startWindowThread()
		while True:
        		capAux = cv2.VideoCapture(number)
        		if not (capAux.isOpened()):
        			break;
     			else:
            			number= number+1
            			capAux.release()
		
		print number
		if number >= 1:   
			self.capture = cv2.VideoCapture(1)
		else:
			self.capture = cv2.VideoCapture(0)

		w = int(self.capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
		h = int(self.capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))

		# video recorder
		fourcc = cv2.cv.CV_FOURCC(*'XVID')
		self.out = cv2.VideoWriter(time.strftime("Videos/%d-%m-%Y_%H:%M:%S")+".avi", fourcc, 25, (w, h))

		if not self.capture.isOpened():
			print chr(27) + "[0;31m" + "FPV system not connected.", ; print chr(27) + "[0m"

		t0 = time.time()
		result= ["Label not found"]

		while self.capture.isOpened() and not self.stopVideoCapture:
			coordY= 50
			ch = 0xFF & cv2.waitKey(1)
			ret, frame = self.capture.read()
			if ret == True:
				for i in result: 
					cv2.putText(frame, i, (20, coordY), cv2. FONT_HERSHEY_DUPLEX, 1.5, (0, 0, 255), 2)
					coordY = coordY + 50

				self.out.write(frame)
        			cv2.imshow("Drone %s" %self.numVehicle,frame)

				timer = round((time.time() - t0) % self.secondsPerPhoto, 2)
				if  timer >= 0 and timer <= 0.99:
            				photoFile = time.strftime("Images/%d-%m-%Y_%H:%M:%S")+".jpg"
            				cv2.imwrite(photoFile, frame)
            				print chr(27) + "[1;33m" + "Drone %s: " %self.numVehicle + photoFile, "saved", ; print chr(27) + "[0m"
					result = self.labelDetection(photoFile, result)	
    			else:
        			break

		self.capture.release()
		self.out.release()
		cv2.destroyWindow("Drone %s" %self.numVehicle)	
		cv2.waitKey(1)
		cv2.waitKey(1)
		cv2.waitKey(1)
		cv2.waitKey(1)
    def run(self):
        # Waits for the image service to become available.
        # This service is set up in camera_srv.py.
        rospy.wait_for_service('last_image_global')

        # Create the image capture function and save image
        last_image_service = rospy.ServiceProxy('last_image_global', ImageSrv)
        self.last_image = self.ros_to_np_img(last_image_service().image_data)

        points = []
        corners = ["bottom left boundary corner", "bottom right boundary corner", "top right boundary corner",
                   "top left corner", "fork goal point", "knife goal point", "spoon goal point"]
        # Callback function taken from lab4 code. Adds a clicked
        # point to the points list.
        def on_mouse_click(event, x, y, flag, param):
            if (event == cv2.EVENT_LBUTTONUP):
                point = (x, y)
                points.append(point)
                if len(points) < 7:
                    print "Click on the " + corners[len(points)] + "."
                else:
                    print "Done calibrating image."

        # Comment out next two lines if debugging (CV2 bug)
        cv2.startWindowThread()
        cv2.namedWindow("CV Image")
        # Display the CV Image
        cv2.imshow("CV Image", self.last_image)

        print "Click on the " + corners[0] + " boundary corner."
        cv2.setMouseCallback("CV Image", on_mouse_click, param=1)

        # Have user choose the four boundary corners for
        # Zumy movement 
        while len(points) < len(corners):
            if rospy.is_shutdown():
                raise KeyboardInterrupt
            cv2.waitKey(10)

        # Exit out of picture
        cv2.destroyAllWindows()
        cv2.waitKey(1)

        # | u1 u2 u3 u4 |
        # | v1 v2 v3 v4 |
        self.boundary_corners = np.array(points[:4]).T
        self.goal_px_locations = {
          'start': [points[0][0], points[0][1]],
          'fork': [points[4][0], points[4][1]],
          'knife': [points[5][0], points[5][1]],
          'spoon': [points[6][0], points[6][1]]
        }
        self.create_homography()

        if self.debug:
            self.check_homography()
            path = self.getPath({'destination': 'fork'})
            print path
        # Done with setup. Wait for path planning requests.
        rospy.spin()
Exemple #5
0
    def __init__(self):

        cv2.namedWindow("Image window", 1)
        self.bridge = CvBridge()
        cv2.startWindowThread()
        self.imgthreshMultiplier = 3

        self.imginfo = [0, 0]  # x y
        self.imgCenter = [0, 0]  # xy

        self.imgThreshold = [0, 0]
        self.imgp = 0.1

        self.headOdom = [0, 0]  # yaw , pitch
        self.imgin = 0
        self.init = 0
        self.habituation = 0
        self.speechCount = 0
        self.undetectedcount = 0
        self.searching = False

        self.state = 'nothing'

        self.memoryProxy = ALProxy("ALMemory", robotIP, 9559)

        # publisher
        self.image_pub = rospy.Publisher("nao_detection", Image, queue_size=10)
        self.behaviorpub = rospy.Publisher('/nao_behavior/add', String, queue_size=5)
        self.headpub = rospy.Publisher('/nao_behavior/head', Float32MultiArray, queue_size=1)

        # subscribers
        self.image_sub = rospy.Subscriber("/camera/image_raw", Image, self.imgCallback)
        self.statesub = rospy.Subscriber("/joint_states", JointState, self.jointstateC)
        self.image = numpy.zeros((160, 120, 3), numpy.uint8)
        self.voiceDetection = rospy.Subscriber("/nao_behavior/speech_detection", Bool, self.speechCallback)
Exemple #6
0
 def __enter__(self):
     print "entering PrintSink"
     print "  starting cv2 window thread"
     cv2.startWindowThread()
     print '  creating cv2 window "input"'
     cv2.namedWindow("input")
     return self
 def get_cams(self, image):
     cv2.namedWindow("Screw")
     cv2.startWindowThread()
     cv2.setMouseCallback("Screw", self._get_screw)
     self.screw_location = Point(0,0)
     while True:
         img = copy.copy(image)
         cv2.circle(img, self.screw_location.to_image(), 3, (0,0,255), 3)
         cv2.imshow('Screw', img)
         k = cv2.waitKey(33)
         if k==10:
             # enter pressed
             break
         elif k==-1:
             pass
         else:
             print k
             print 'Press Enter to continue..'
     cv2.destroyWindow('Screw')
     arc = self.screw_location - self.board_center
     cam_angle = arc.angle()
     self.cam_angle = cam_angle
     if len(self.base_cams) != 0:
         self.cams = self.rotate(self.base_cams, cam_angle)
         # self.cams = [c for c in self.cams if c.y <= 0]
         self.locate_cams(image)
def showImage(name, image):
    cv2.namedWindow(name, cv2.WINDOW_AUTOSIZE)
    cv2.startWindowThread()
    cv2.imshow(name, image)
    cv2.waitKey(0)
    cv2.destroyWindow(name)
    cv2.waitKey(1)
Exemple #9
0
    def onScreen(self, degrees=None, scaleFactor=1):
        """I need something to display these things for debugging. This uses
        OpenCV to display in a no-frills, any-key-to-dismiss window."""

        if degrees:
            im = Image.fromarray(self.rotate(degrees))
            msg = "{} degree rotation - any key/click to continue".format(degrees)
        else:
            im = Image.fromarray(self.array)
            msg = "any key/click to continue"

        caption = "image display - any key to close"

        cv2.namedWindow(caption, 0)
        shape = im.shape
        width = int(shape[1] * scaleFactor)
        height = int(shape[0] * scaleFactor)

        cv2.resizeWindow(caption, width, height)
        # cv2.setMouseCallback(caption, kill_window)
        
        cv2.imshow(caption, im)
        cv2.startWindowThread()
        cv2.waitKey(0)
        cv2.destroyWindow(caption)
Exemple #10
0
def takePic(path):
    lastTime = time.time()
    video_capture = cv2.VideoCapture(0)
    cv2.startWindowThread()
    cv2.namedWindow('Photobooth')
    faceCascade = cv2.CascadeClassifier(cascadePath)
    i = 0 #img number
    while(True):
        ret, frame = video_capture.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(
                gray,
                scaleFactor=1.1,
                minNeighbors=10,
                minSize=(50,50),
                flags=cv2.CASCADE_SCALE_IMAGE
            )

        for (x, y, w, h) in faces:
            face = gray[y:y+h, x:x+w]
            cv2.rectangle(frame, (x,y), (x+w, y+h), (0,0,255),2)

        cv2.imshow('Photobooth', frame)

        if time.time() - lastTime>5:
            cv2.imwrite(path + '/' + str(i) + '.jpg', face)
            lastTime = time.time()
            i+=1
    video_capture.release()
    cv2.destroyAllWindows()
def resizeImage(file_path): 
    #Resize Cropping & Padding an image to the 640x480 pixel size
    if file_path is not -1:
        if check_image_with_pil(file_path):
            image = Image.open(file_path)
            image.thumbnail(size, Image.ANTIALIAS)
            image_size = image.size

            padding_0 = max( (size[0] - image_size[0]) / 2, 0 )
            padding_1 = max( (size[1] - image_size[1]) / 2, 0 )
            cv2.namedWindow('Original Image')
            cv2.namedWindow('Resized Image')
            cv2.startWindowThread()
            orig_img = cv2.imread(file_path, 0)
            cv2.imshow('Original Image',orig_img)
            cv2.waitKey(2)

            if((padding_0==0) & (padding_1==0)):
                image.save(file_path, img_save_type)
            else:
                thumb = image.crop( (0, 0, size[0], size[1]) )
                thumb = ImageChops.offset(thumb, int(padding_0), int(padding_1))
                thumb.save(file_path)

            resized_img = cv2.imread(file_path, 0)
            cv2.imshow('Resized Image',resized_img)
    else :
        cv2.destroyAllWindows()
        cv2.waitKey(2)
Exemple #12
0
    def onScreen(self, args=dict()):
        """I need something to display these things for debugging. This uses
        OpenCV to display in a no-frills, any-key-to-dismiss window."""

        if 'windowHeight' not in args:
            args['windowHeight'] = 500

        if 'delayAutoClose' not in args:
            args['delayAutoClose'] = 10000

        if 'scaleFactor' not in args:
            height = float(self.data['spatialShape'][0])
            args['scaleFactor'] = float(args['windowHeight'])/height

        if 'message' not in args:
            args['message'] = "image display - any key to close"

        # def kill_window(event):
        #     cv2.destroyWindow(args['message'])

        cv2.namedWindow(args['message'], 0)
        shape = self.data['spatialShape']
        width = int(shape[1] * args['scaleFactor'])
        height = int(shape[0] * args['scaleFactor'])

        cv2.resizeWindow(args['message'], width, height)
        # cv2.setMouseCallback(args['message'], kill_window)

        cv2.imshow(args['message'], self.array)
        cv2.startWindowThread()
        cv2.waitKey(args['delayAutoClose'])
        cv2.destroyWindow(args['message'])
def encrypt_video_file_cat_map_stream(rounds, p, q):
	numpy.set_printoptions(threshold='nan')
	video = cv2.VideoCapture(0)

	success, image = video.read()

	width, height, depth = image.shape

	# find the largest dimension
	max_dimension = max(height, width)

	cv2.startWindowThread()
	cv2.namedWindow("enc1", cv.CV_WINDOW_AUTOSIZE)
	cv2.namedWindow("dec1", cv.CV_WINDOW_AUTOSIZE)

	while success:
		# resize the image array to the largest dimension using black pixels
		image.resize(max_dimension, max_dimension, depth)
		# encrypt the image
		arnold_chaotic_map(image, rounds, p, q)
		# stream the encrypted image
		cv2.imshow("enc1", image)

		# decrypt the image
		reverse_arnold_chaotic_map(image, rounds, p, q)
		# resize to the original size
		image.resize((width, height, depth))
		# stream the decoded
		cv2.imshow("dec1", image)

		success, image = video.read()


	return (width, height)
Exemple #14
0
def loadpic(picaddress):
    '''
    loads a picture using opencv 
    pressing 'q' should close the windows, this doesnt work yet

    input: path to the picture to load
    '''

    #read in the image
    img = cv2.imread(picaddress)

    #building windows
    cv2.namedWindow('original', cv2.CV_WINDOW_AUTOSIZE)
    cv2.namedWindow('process1', cv2.CV_WINDOW_AUTOSIZE)

    #start a thread to keep track of events. Without this, we cannot close the  window
    cv2.startWindowThread()

    #show the image
    cv2.imshow('original', img)
    
    #k = cv2.waitKey(0) # for 32 bit machine
    k = cv2.waitKey(0) & 0xFF  #for 64 bit machine

    if k==ord('c'):
        cv2.destroyAllWindows()
        cv2.destroyWindow('original')
        cv2.waitKey(1)
    elif k == ord('q'):
        cv2.destroyAllWindows()
        cv2.destroyWindow('original')
        cv2.waitKey(1)
 def show(self):
     if(self.img is None):
         raise NoImageDefined()
     cv2.namedWindow(self.windowname,0)
     
     cv2.imshow(self.windowname, self.img)
     cv2.startWindowThread()
Exemple #16
0
    def __init__(self, rom_file, frame_skip=1, viz=0):
        """
        :param rom_file: path to the rom
        :param frame_skip: skip every k frames
        :param viz: the delay. visualize the game while running. 0 to disable
        """
        self.ale = ALEInterface()
        self.rng = get_rng(self)

        self.ale.setInt("random_seed", self.rng.randint(self.rng.randint(0, 1000)))
        self.ale.setInt("frame_skip", frame_skip)
        self.ale.loadROM(rom_file)
        self.width, self.height = self.ale.getScreenDims()
        self.actions = self.ale.getMinimalActionSet()

        if isinstance(viz, int):
            viz = float(viz)
        self.viz = viz
        self.romname = os.path.basename(rom_file)
        if self.viz and isinstance(self.viz, float):
            cv2.startWindowThread()
            cv2.namedWindow(self.romname)

        self._reset()
        self.last_image = self._grab_raw_image()
        self.framenum = 0
def takeTracker():
    cv2.startWindowThread()
    cv2.namedWindow("InstaPy - InstaPhoto Tracker", 1)
    cv2.setMouseCallback("InstaPy - InstaPhoto Tracker", getPixelRGB)
    img.save('InstaPy Album/InstaPhotoTrack.png')
    image = cv2.imread("InstaPy Album/InstaPhotoTrack.png")
    cv2.imshow("InstaPy - InstaPhoto Tracker", image)
def CreateMashup():
    global mashupPressed
    blank_image = Image.new("RGB", (1290, 970))
    _, frame = cap.read()
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    img = Image.fromarray(cv2image)
    blank_image.paste(img, (0,0))

    cv2image = cv2.cvtColor(frame, cv2.COLOR_RGB2HLS)
    img = Image.fromarray(cv2image)
    blank_image.paste(img, (650,0))

    cv2image = cv2.cvtColor(frame, cv2.COLOR_RGB2YUV)
    img = Image.fromarray(cv2image)
    blank_image.paste(img, (0,490))

    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    img = Image.fromarray(cv2image)
    blank_image.paste(img, (650,490))

    blank_image.save("InstaPy Album/mashup.png")
    mashupPressed = True

    cv2.startWindowThread()
    cv2.namedWindow("Mashup",1)
    image = cv2.imread("InstaPy Album/mashup.png")
    image = cv2.resize(image, (0,0), fx=0.70, fy=0.70)
    cv2.imshow("Mashup", image)
Exemple #19
0
def show_binary_image(image_bin, window_name, save = False, break_point = 0,\
                        max = 255):
   """Displays a binary image, optionally saves it to the current directory.
   Parameters
   ----------
   image_bin : numpy matrix of integers
      The binary image that is asked to be displayed.
   window_name : string
      The name of the display window.
   save : boolean
      If the value is True then the binary image will be saved as a file.
   break_point : int
      Any pixel with a value greater than this value will be set to max
      while the rest will be set to 0.
   max_value : int
      Any pixel with a value greater than break_point will be set to this
      number while the rest will be set to 0.
   Returns
   -------
   None
   """
   image_show = get_binary_image(image_bin, break_point, 255)
   cv2.startWindowThread()
   cv2.namedWindow(window_name)
   cv2.imshow(window_name, image_show)
   cv2.waitKey(1)
   if save:
      cv2.imwrite(window_name + SUFFIX, image_show)
    def __init__(self):
        cv2.namedWindow("Live Image", 1)
        cv2.namedWindow("Base Image", 1)
        cv2.namedWindow("Original Image", 1)
        cv2.startWindowThread()
        self.bridge = CvBridge()

        image_sub = Subscriber(
            "/camera/rgb/image_color",
            Image,
            queue_size=1
        )

        person_sub = Subscriber(
            "/upper_body_detector/detections",
            UpperBodyDetector,
            queue_size=1
        )

        depth_sub = Subscriber(
            "/camera/depth/image_rect",
            Image,
            queue_size=1
        )
        #        detections_image_sub = Subscriber(
        #            "/upper_body_detector/image",
        #            Image,
        #            queue_size=1
        #        )

        ts = ApproximateTimeSynchronizer([image_sub, person_sub, depth_sub], 1, 0.1)
        ts.registerCallback(self.image_callback)
 def test_find_holes(self):
     ft = FishTracker()
     BAG_NAME = '../../fish_detective/bags/2016-04-06-16-57-50.bag' #1437
     # BAG_NAME = '../../fish_detective/training/bags/three_fish.bag'
     bag = rosbag.Bag(BAG_NAME)
     imgs = self.get_ros_img(bag)
     zero = imgs.next()
     # import IPython; IPython.embed()
     ft.find_holes(zero)
     fishes = ft.track_holes(zero)
     cv2.namedWindow("Calibration")
     cv2.startWindowThread()
     cv2.imshow('Calibration', fishes)
     k = cv2.waitKey(0)
     one = imgs.next()
     ft.find_holes(one)
     fishes = ft.track_holes(one)
     cv2.imshow('Calibration', fishes)
     k = cv2.waitKey(0)
     for i in range(60):
         im = imgs.next()
         ft.find_holes(im)
         fishes = ft.track_holes(im)
         cv2.imshow('Calibration', fishes)
         k = cv2.waitKey(1)
     cv2.destroyWindow('Calibration')
Exemple #22
0
    def __init__(self):

        cv2.namedWindow("Image window", 1)
        cv2.startWindowThread()
        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("/usb_cam/image_raw",
                                          Image, self.callback)
Exemple #23
0
def main():
    
    rospy.init_node(node_name) # check to see if launch file remaps this
    try:
        cv2.namedWindow(win1_name, flags=cv2.cv.WINDOW_NORMAL) #see if flag is necessary
        # cv2.NamedWindow(win2_name, flags=WINDOW_NORMAL)
        cv2.moveWindow(win1_name,200,0)
        # cv2.MoveWindow(win2_name,200,300)
        cv2.resizeWindow(win1_name,100,300)
        # cv2.ResizeWindow(win2_name,100,300)

        cv2.startWindowThread()
        rospy.on_shutdown(cleanup)
        
        
        # rate = rospy.Rate(freq)
        sub_RBG = rospy.Subscriber("/camera/rgb/image_color", Image, callback_rbg)
        # sub_depth = rospy.Subscriber("/camera/depth/image_raw", Image, callback_depth)

        print('==========Initializing Subscribers=====')
        rospy.sleep(1)

        # while not rospy.is_shutdown():
        #     rospy.rate=1
        #     rospy.wait_for_message("/camera/rgb/image_color", Image, timeout=None)
        #     k = cv2.waitKey(0) & 0xFF
        #     if k==27:
        #         cleanup()
        #     # rate.sleep()
        rospy.spin()

    except KeyboardInterrupt:
        print "Shutting down"
        cleanup()
Exemple #24
0
def getIndividualColour(h,s,v, colourname):

    cv2.startWindowThread()

    # Create a black image, a window
    img = np.zeros((300,512,3), np.uint8)
    cv2.namedWindow('image')

    # create trackbars for color change
    cv2.createTrackbar('H','image',h,179,nothing)
    cv2.createTrackbar('S','image',s,255,nothing)
    cv2.createTrackbar('V','image',v,255,nothing)

    # create tracker for which colour value we want to set
    # i.e. 1= blue, 2 = red, etc.

    while(1):
        img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
        cv2.imshow('image',img)
        k = cv2.waitKey(10) & 0xFF
        if k == 27:
            break
        if k == 97:
            print("Returning values of [" + str(r) + ", " + str(g) + ", " + str(b) + "] for colour \"" + colourname + "\".")
        # get current positions of four trackbars
        r = cv2.getTrackbarPos('H','image')
        g = cv2.getTrackbarPos('S','image')
        b = cv2.getTrackbarPos('V','image')
        img[:] = [r,g,b]

    cv2.destroyWindow('image')
    cv2.waitKey(1)
    return [r,g,b]
Exemple #25
0
    def __init__(self):

        namedWindow("Image window", 1)
        namedWindow("blur", 1)
        self.bridge = CvBridge()
        startWindowThread()
        self.image_sub = rospy.Subscriber("/turtlebot_1/camera/rgb/image_raw",
                                          Image, self.callback)
Exemple #26
0
 def displayImage(self, windowName="Look at lines of interest"):
     image = self.imageWithLines()
     # Display the image and disappear when a key is hit
     cv2.namedWindow(windowName, cv2.WINDOW_AUTOSIZE)
     cv2.startWindowThread()
     cv2.imshow(windowName, image)
     cv2.waitKey(0)
     cv2.destroyAllWindows()
Exemple #27
0
 def recivedEvent(self, notifyId):
     if notifyId ==  ID_NOTIFY_LOADED_VIDEO:
         im = self.__presenter.getPreviewImage()
         print(im)
         if im is not None:
             cv2.startWindowThread()
             cv2.namedWindow("preview")
             cv2.imshow("preview", im)
Exemple #28
0
def main():
    rospy.init_node("depth_view")
    rospy.Subscriber("image", Image, new_image)

    cv2.namedWindow(WINDOW_NAME)
    cv2.startWindowThread()

    rospy.spin()
Exemple #29
0
    def __init__(self, rom_file, viz=0, height_range=(None,None),
            frame_skip=4, image_shape=(84, 84), nullop_start=30,
            live_lost_as_eoe=True):
        """
        :param rom_file: path to the rom
        :param frame_skip: skip every k frames and repeat the action
        :param image_shape: (w, h)
        :param height_range: (h1, h2) to cut
        :param viz: visualization to be done.
            Set to 0 to disable.
            Set to a positive number to be the delay between frames to show.
            Set to a string to be a directory to store frames.
        :param nullop_start: start with random number of null ops
        :param live_losts_as_eoe: consider lost of lives as end of episode.  useful for training.
        """
        super(AtariPlayer, self).__init__()
        self.ale = ALEInterface()
        self.rng = get_rng(self)

        self.ale.setInt("random_seed", self.rng.randint(0, 10000))
        self.ale.setBool("showinfo", False)

        try:
            ALEInterface.setLoggerMode(ALEInterface.Logger.Warning)
        except AttributeError:
            log_once()

        self.ale.setInt("frame_skip", 1)
        self.ale.setBool('color_averaging', False)
        # manual.pdf suggests otherwise. may need to check
        self.ale.setFloat('repeat_action_probability', 0.0)

        # viz setup
        if isinstance(viz, six.string_types):
            assert os.path.isdir(viz), viz
            self.ale.setString('record_screen_dir', viz)
            viz = 0
        if isinstance(viz, int):
            viz = float(viz)
        self.viz = viz
        if self.viz and isinstance(self.viz, float):
            self.windowname = os.path.basename(rom_file)
            cv2.startWindowThread()
            cv2.namedWindow(self.windowname)

        self.ale.loadROM(rom_file)
        self.width, self.height = self.ale.getScreenDims()
        self.actions = self.ale.getMinimalActionSet()


        self.live_lost_as_eoe = live_lost_as_eoe
        self.frame_skip = frame_skip
        self.nullop_start = nullop_start
        self.height_range = height_range
        self.image_shape = image_shape

        self.current_episode_score = StatCounter()
        self.restart_episode()
Exemple #30
0
    def set_user_params(self):
        '''Is responsible for displaying each image of the data point folder.

        Works with self.paramsUI to gather input from the user'''
        self.stage = 0
        global TESTING
        for i in range(len(self.imgs)):
            if TESTING and i != 0:
                self.user_params[i]['testing_region'] = self.user_params[0]['testing_region']
            else:
                self.i = i
                self.drawing_overlay = self.imgs[i].__copy__()

                if sys.platform.startswith('win'):
                    import ctypes
                    user32 = ctypes.windll.user32
                    screen_res = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
                else:
                    status, output = commands.getstatusoutput("xrandr | grep '*'")
                    if not status:
                        screen_res = tuple(
                                           [int(dim) for dim in \
                                           [
                                            part for part in output.split('\n')[-1].split(' ') if part][0].split('x')
                                            ]
                                           )
                    else:
                        screen_res = 800, 600

                scale_width = screen_res[0] / float(self.imgs[i].shape[1])
                scale_height = screen_res[1] / float(self.imgs[i].shape[0])
                scale = min(scale_width, scale_height)
                window_width = int(self.imgs[i].shape[1] * scale)
                window_height = int(self.imgs[i].shape[0] * scale)

                cv2.namedWindow(self.img_names[i], cv2.WINDOW_NORMAL)
                cv2.startWindowThread()
                cv2.resizeWindow(self.img_names[i], window_width, window_height)

                self.ix, self.iy = -1, -1
                if i == 0:
                    cv2.putText(self.drawing_overlay, 'Select testing region', (20, 100), \
                            cv2.FONT_HERSHEY_COMPLEX, 2, (255, 255, 0), 5)
                else:
                    cv2.putText(self.drawing_overlay, 'Place testing region', (20, 100), \
                            cv2.FONT_HERSHEY_COMPLEX, 2, (255, 255, 0), 5)
                cv2.setMouseCallback(self.img_names[i], self.paramsUI)
                cv2.imshow(self.img_names[i], self.drawing_overlay)
                while True:
                    k = cv2.waitKey(5)
                    if k == 27:
                        print 'User Interrupt: exiting'
                        exit()
                    elif k == ord('n'):
                        self.stage = 0
                        cv2.destroyWindow(self.img_names[i])
                        cv2.waitKey(1)
                        break
def _main_():

    config_path = 'config_cargo_door.json'
    weights_path = '/media/ubuntu/hdd/tensorflow_data/YOLO/CargoDoor/full_yolo_cargo_door.h5'
    image_path = '/media/ubuntu/DANFOSS/airport_images.tar.gz/015.jpg'

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(architecture=config['model']['architecture'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################

    print(weights_path)
    yolo.load_weights(weights_path)

    ###############################
    #   Predict bounding boxes
    ###############################

    cv2.namedWindow("Detection", cv2.WINDOW_NORMAL)
    cv2.startWindowThread()  #to make sure we can close it later on

    if image_path[-4:] == '.mp4':
        video_reader = cv2.VideoCapture(image_path)

        nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
        frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
        frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))

        for i in range(nb_frames):
            _, image = video_reader.read()

            boxes = yolo.predict(image)
            image = draw_boxes(image, boxes, config['model']['labels'])

            print len(boxes), 'box(es) found'

            #display frame
            image_np = np.uint8(image)
            cv2.imshow("Detection", image_np)
            k = cv2.waitKey(0) & 0xEFFFFF
            if k == 27:
                print("You Pressed Escape")
                break

        video_reader.release()
    else:
        image = cv2.imread(image_path)

        #Predict and time it
        t0 = time.time()
        boxes = yolo.predict(image)
        t1 = time.time()
        total = t1 - t0

        #overlay boxes
        image = draw_boxes(image, boxes, config['model']['labels'])

        #feedback
        print len(boxes), 'box(es) found'
        print 'Prediciton took %f seconds' % (total)

        #display frame
        cv2.imshow("Detection", image)
        k = cv2.waitKey(0) & 0xEFFFFF
        if k == 27:
            print("You Pressed Escape")

    cv2.destroyAllWindows()
    for i in range(1, 5):
        cv2.waitKey(1)
Exemple #32
0
def detect_and_track_multiple_faces():

    # open cam device
    cam = cv2.VideoCapture(1)

    # create 2 opencv named windows
    cv2.namedWindow('base-image', cv2.WINDOW_AUTOSIZE)
    cv2.namedWindow('result-image', cv2.WINDOW_AUTOSIZE)

    # position the windows next to eachother
    cv2.moveWindow('base-image', 0, 100) #x, y
    cv2.moveWindow('result-image', 400, 100)

    # start window thread for 2 windows
    cv2.startWindowThread()

    # color of rect we draw around face
    rect_color = (0, 165, 255)

    # var holding cur frame, cur faceId
    frame_counter = 0
    cur_face_id = 0

    # var holding correlation trackers, and name per faceId
    face_trackers = {} # fid => tracker
    face_names = {}

    try:
        while True:

            # retrieve latest img from webcam
            time_start = time.time()
            ok, full_size_base_img = cam.read()

            # resize img to 320x240
            base_img = cv2.resize(full_size_base_img, (320, 240))

            # check if a key press, if it was q, destroy all
            # windows
            k = cv2.waitKey(2)
            if k == ord('q'):
                break

            # result img, the one we show user
            # result = original + rects
            result_img = base_img.copy()


            # STEPS:
            # * Update all trackers and remove the ones
            #   that are not relevant anymore
            # * Every 10 frames:
            #       + Use face detection on cur frame
            #           and look for faces
            #       + For each found face, check if centerpoint
            #           is within existing tracked box. If so,
            #           do nothing
            #       + If centerpoint is NOT in existing tracked box,
            #           then we add a new tracker with a new face-id



            # increase frame counter
            frame_counter += 1


            # update all trackers and remove the ones for which
            # the update indicated the quality was not good enough
            fids_to_delete = []
            for fid in face_trackers.keys():
                tracking_quality = face_trackers[fid].update(base_img)

                # if track quality is NOT good enough, we
                # must del this tracker
                if tracking_quality < 7:
                    fids_to_delete.append(fid)

            for fid in fids_to_delete:
                print("Remove fid: " + str(fid) + " from list of trackers")
                face_trackers.pop(fid, None)



            # Every 10 frames, determine which faces
            # are present in frame
            if (frame_counter % 10) == 0:

                # for face detection, use gray color image
                gray = cv2.cvtColor(base_img, cv2.COLOR_BGR2GRAY)

                # now, use haar cascade to find all faces
                # in image
                faces = faceCascade.detectMultiScale(gray, 1.3, 5)

                # loop over all faces
                for (_x, _y, _w, _h) in faces:
                    x = int(_x)
                    y = int(_y)
                    w = int(_w)
                    h = int(_h)

                    # cal centerpoint
                    x_bar = x + 0.5*w
                    y_bar = y + 0.5*h

                    # var holding infor which faceId we matched with
                    match_fid = None

                    # now loop over all trackers and check if
                    # center point of the face is within
                    # box of a tracker
                    for fid in face_trackers.keys():
                        track_position = face_trackers[fid].get_position()

                        t_x = int(track_position.left())
                        t_y = int(track_position.top())
                        t_w = int(track_position.width())
                        t_h = int(track_position.height())

                        # cal centerpoint
                        t_x_bar = t_x + 0.5*t_w
                        t_y_bar = t_y + 0.5*t_h

                        # check if center point of face is within
                        # the rect of a tracker region.
                        # Also, the centerpoint of tracker region must
                        # be within the region detected as a face
                        # if both of these conditions hold
                        # we have a match
                        if (
                            # center of face
                            (t_x <= x_bar <= (t_x + t_w)) and
                            (t_y <= y_bar <= (t_y + t_h)) and
                            # center of track
                            (x <= t_x_bar <= (x + w)) and
                            (y <= t_y_bar <= (y + h))
                        ):
                            match_fid = fid

                    # if no matched fid, then have to create a new tracker
                    if match_fid is None:
                        print("create a new tracker " + str(cur_face_id))

                        # create and store the tracker
                        tracker = dlib.correlation_tracker()
                        tracker.start_track(base_img,
                                            dlib.rectangle(
                                                x - 10,
                                                y - 20,
                                                x + w + 10,
                                                y + h + 20
                                            ))
                        face_trackers[cur_face_id] = tracker

                        # start a new thread, to simulate face recognition
                        # not yet implemented in this version
                        # t = threading.Thread(
                        #     target= recognize_person,
                        #     args = (face_names, cur_face_id)
                        # )
                        # t.start()

                        # increase curfaceId counter
                        cur_face_id += 1

            # end 10 frame-if

            # now loop over all trackers and raw rect
            # around detected faces.
            for fid in face_trackers.keys():
                track_position = face_trackers[fid].get_position()

                t_x = int(track_position.left())
                t_y = int(track_position.top())
                t_w = int(track_position.width())
                t_h = int(track_position.height())

                cv2.rectangle(result_img,
                              (t_x, t_y),
                              (t_x + t_w, t_y + t_h),
                              rect_color, 2)

                # write name ...
                # not implemented yet

            # show large result
            large_result = cv2.resize(result_img,
                                      (OUTPUT_SIZE_WIDTH, OUTPUT_SIZE_HEIGHT))

            # finally, show images on screen
            time_end = time.time()
            fps = int(1/(time_end-time_start))
            fps_display = "fps: " + str(fps)
            cv2.putText(large_result, fps_display, (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

            cv2.imshow('base-image', base_img)
            cv2.imshow('result-image', large_result)

    # if user press Ctrol-C in console
    # break out of main loop
    except KeyboardInterrupt as e:
        pass

    # destroy openCV windows
    cv2.destroyAllWindows()
    exit(0)
Exemple #33
0
def refine(facial_feature, thresh=0, os=mac):
    """
    Args:
        dir:    (string) directory of one group of data
        mode:   (string) decide whether to move or click to change the joint point
    """
    mode = 'drag'
    anno = AnnotationLoader(facial_feature, threshold=thresh)
    # app = MyCollectApp()
    anno.window_name = 'refining'
    cv2.namedWindow(anno.window_name)
    idx = 0
    while idx < len(anno.data_files):
        anno.cur_file = anno.data_files[idx]
        # if anno.data_files[idx]
        # anno.window_name = dir.split('/')[-1] + '/' + anno.cur_file
        anno.img = anno.lood_img(idx)
        # anno.img -= anno.img #  To show black canvas
        img = anno.img.copy()
        # img = np.zeros((360, 640, 3))
        # cv2.namedWindow(anno.window_name)
        anno.plot_skeleton(img, anno.cur_file, thick=2)
        if mode == 'drag':
            cv2.setMouseCallback(anno.window_name, anno.MouseCallback_drag)
        elif mode == 'click':
            cv2.setMouseCallback(anno.window_name, anno.MouseCallback_click)
        else:
            print("No mode named:" + mode)
        cv2.startWindowThread()
        cv2.imshow(anno.window_name, img)
        while True:
            # By judging status, the program could be more responsive.
            if anno.deleting:
                if cv2.waitKey(10) == os:  # Backspace
                    del anno.annotation[anno.cur_file][anno.person_selected]
                    anno.deleting = False
                    anno.person_selected = -1
                    anno.joint_selected = ''
                    img = anno.img.copy(
                    )  # clear all skeleton drawn on the image
                    anno.plot_skeleton(img, anno.cur_file, thick=2)
                    cv2.imshow(anno.window_name, img)
            else:
                key = cv2.waitKey(10)
                if key == 13 or key == 3 or key == 100:  # Enter or D button
                    anno.joint_selected = False
                    anno.revise(anno.cur_file)
                    idx += 1
                    break
                elif key == 2 or key == 97:
                    if idx >= 1:
                        idx -= 1
                    break
                elif key == 32:  # Space
                    global add_joint_num
                    anno.adding = True
                    anno.add_joint_num = int(
                        input("Please input the joint number:")) + 1

                    # pop_box()
                    # anno.add_joint_num = int(anno.add_joint_num) + 1
                    # add_joint_num = ''

    # anno.revise()
    cv2.destroyAllWindows()
    cv2.waitKey(1)
    cv2.waitKey(1)
    cv2.waitKey(1)
    cv2.waitKey(1)
Exemple #34
0
    for contour in contours:
        area = cv2.contourArea(contour)
        if area > max_area:
            max_area = area
            best_contour = contour

    M = cv2.moments(best_contour)
    cx, cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])

    cv2.circle(blur, (cx, cy), 10, (0, 0, 255), -1)

    
    ###########

    #cv2.imshow("Frame", blur) #displays the image
    cv2.imshow("Frame - Threshold", thresh2)
    #cv2.imshow("frame", image) #shows contours
    cv2.startWindowThread() #has to be used in order to actually close the window
    key = cv2.waitKey(1) & 0xFF #has to be used in conjunction with imshow, to close window
    
    rawCapture.seek(0) #goes to the first image in the array
    rawCapture.truncate(0) #deletes previously seen image

    #closes the program when you hit q
    if key == ord("q"):
        cv2.destroyAllWindows()
        break

    #actual image stuff
    
Exemple #35
0
            self.car_pos_dir,
            'car_pos' + str('{:06d}'.format(self.img_count)) + '.yaml')

        cv2.imwrite(cameraFront_img_name, self.cameraFront_img)
        cv2.imwrite(cameraRight_img_name, self.cameraRight_img)
        cv2.imwrite(cameraBack_img_name, self.cameraBack_img)
        cv2.imwrite(cameraLeft_img_name, self.cameraLeft_img)
        cv2.imwrite(cameraAll_img_name, self.cameraAll)

        car_pose_stream = open(car_pose_name, "w")
        car_data = {
            'car_pos': self.car_pos.tolist(),
            'car_ori': self.car_ori.tolist()
        }
        yaml.dump(car_data, car_pose_stream)
        self.lock.release()

        self.img_count += 1

        if self.img_count > 100000:
            sys.exit()


if __name__ == "__main__":
    rospy.init_node('save_pics_sync')
    cv2.startWindowThread()
    ss = saver()
    rate = rospy.Rate(50)
    while not rospy.is_shutdown():
        ss.save_pics()
Exemple #36
0
def detect(url_or_np_array):

    # check if i get a url (= string) or np.ndarray
    if isinstance(url_or_np_array, basestring):
        try:
            response = requests.get(url_or_np_array, timeout=10)  # download
            full_image = cv2.imdecode(np.asarray(bytearray(response.content)),
                                      1)
        except:
            print "couldn't open link"
            return None
    elif type(url_or_np_array) == np.ndarray:
        full_image = url_or_np_array
    else:
        return None

    if full_image is None:
        print "not a good image"
        return None

    # faces = find_face_dlib(full_image, 1)
    #
    # if not faces["are_faces"]:
    #     print "didn't find any faces"
    #     return None

    image = imutils.resize(full_image, width=500)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # detect faces in the grayscale image
    rects = detector(gray, 1)

    faces_list = [[rect.left(),
                   rect.top(),
                   rect.width(),
                   rect.height()] for rect in list(rects)]
    if not len(faces_list):
        print "didn't find a face!"
        return

    print "rects: {}".format(faces_list)

    # loop over the face detections
    for (i, rect) in enumerate(rects):
        # determine the facial landmarks for the face region, then
        # convert the facial landmark (x, y)-coordinates to a NumPy
        # array
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)

        # convert dlib's rectangle to a OpenCV-style bounding box
        # [i.e., (x, y, w, h)], then draw the face bounding box
        (x, y, w, h) = face_utils.rect_to_bb(rect)
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

        # show the face number
        cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

        # loop over the (x, y)-coordinates for the facial landmarks
        # and draw them on the image
        for j, (x, y) in enumerate(shape):
            if j + 1 in eyes_landmarks:
                cv2.circle(image, (x, y), 1, (255, 0, 0), -1)
                eyes_dict[j + 1] = (x, y)
            else:
                cv2.circle(image, (x, y), 1, (0, 0, 255), -1)

        # cv2.line(image, eyes_dict[38], eyes_dict[41], (255, 0, 0), 2)
        # cv2.line(image, eyes_dict[39], eyes_dict[42], (255, 0, 0), 2)
        # cv2.line(image, eyes_dict[44], eyes_dict[47], (255, 0, 0), 2)
        # cv2.line(image, eyes_dict[45], eyes_dict[48], (255, 0, 0), 2)

        left_eye_x, left_eye_y = int(
            np.mean([
                eyes_dict[38][0], eyes_dict[39][0], eyes_dict[41][0],
                eyes_dict[42][0]
            ])), int(
                np.mean([
                    eyes_dict[38][1], eyes_dict[39][1], eyes_dict[41][1],
                    eyes_dict[42][1]
                ]))
        right_eye_x, right_eye_y = int(
            np.mean([
                eyes_dict[44][0], eyes_dict[45][0], eyes_dict[47][0],
                eyes_dict[48][0]
            ])), int(
                np.mean([
                    eyes_dict[44][1], eyes_dict[45][1], eyes_dict[47][1],
                    eyes_dict[48][1]
                ]))

        cv2.circle(image, (left_eye_x, left_eye_y), 1, (0, 255, 0), -1)
        cv2.circle(image, (right_eye_x, right_eye_y), 1, (0, 255, 0), -1)

        left_eye = np.array((left_eye_x, left_eye_y))
        right_eye = np.array((right_eye_x, right_eye_y))

        print "distance between eyes: {}".format(
            np.linalg.norm((right_eye) - (left_eye)))
        # print "distance between eyes: {}".format(np.sqrt(np.sum(((right_eye_x, right_eye_y) - (left_eye_x, left_eye_y))**2)))

    # print cv2.imwrite("/data/yonatan/linked_to_web/face_landmarks/image3.jpg", image)

    cv2.namedWindow("Output", cv2.CV_WINDOW_AUTOSIZE)
    cv2.startWindowThread()

    cv2.imshow("Output", image)
    cv2.waitKey(0)
Exemple #37
0
def click_estimate_diameter():
    newWindow = Toplevel(root)
    newWindow.geometry('600x600+800+100')
    newWindow.iconbitmap("e:\cnts_sem\icon3.ico")
    newWindow.attributes("-topmost", True)
    ifile = filedialog.askopenfilename(initialdir="/", title="Select a file",
                                       filetypes=(("png files", "*.png"), ("all files", "*.*")))
    path = ifile
    im = cv2.imread(path)
    #print(len(im), len(im[0]))
    boxes1 = []
    def on_mouse_0(event, x, y, flags, params):
        if event == cv2.EVENT_LBUTTONDOWN:
            #print('Start Mouse Position: ' + str(x) + ', ' + str(y))
            sbox = [x, y]
            boxes1.append(sbox)
            cv2.circle(im, (x, y), radius=0, color=(0, 0, 255), thickness=5)
            #print(boxes)
    cv2.startWindowThread()
    cv2.namedWindow('Select scale', cv2.WND_PROP_FULLSCREEN)

    cv2.setMouseCallback('Select scale', on_mouse_0, 0)
    while True:
        cv2.imshow('Select scale', im)
        if cv2.waitKey(10) == 27:
            break
    cv2.waitKey(1)
    cv2.destroyAllWindows()
    scale = math.sqrt( ((boxes1[0][0]-boxes1[1][0])**2)+((boxes1[0][1]-boxes1[1][1])**2) )
    print(scale)
    result = 0

    boxes = []
    def on_mouse(event, x, y, flags, params):
        if event == cv2.EVENT_LBUTTONDOWN:
            #print('Start Mouse Position: ' + str(x) + ', ' + str(y))
            sbox = [x, y]
            boxes.append(sbox)
            cv2.circle(im, (x, y), radius=0, color=(0, 0, 255), thickness=5)
            #print(boxes)
    cv2.startWindowThread()
    cv2.namedWindow('Select minimum diameter')

    cv2.setMouseCallback('Select minimum diameter', on_mouse, 0)
    while True:
        cv2.imshow('Select minimum diameter', im)
        if cv2.waitKey(10) == 27:
            break
    cv2.waitKey(1)
    cv2.destroyAllWindows()
    min_distance = math.sqrt( ((boxes[0][0]-boxes[1][0])**2)+((boxes[0][1]-boxes[1][1])**2) )
    print(min_distance)

    selector_s = Label(newWindow, text="Deleting irrelevant parts...", font='12')
    selector_s.pack()
    newWindow.update()
    im = metadata_removal(im)
    im = eight_bit(im)
    gray = grayscale(im)
    equ = histogram_equalization(gray)
    median = median_filtering(equ)
    otsu = otsu_method(median)
    border = add_border(otsu)
    reverse = reversing(border)
    opening = opening_operation(reverse, np.ones((6, 5), np.uint8))
    im = closing_operation(opening, np.ones((6, 5), np.uint8))

    # list of all images
    t0 = time.time()
    selector = Label(newWindow, text="> Performing detections...",font = '12')
    selector.pack()
    newWindow.update()
    print('Starting estimations...')
    total_x_0 = np.empty((len(im) ,len(im[0])))
    total_x_225 = np.empty((len(im) ,len(im[0])))
    total_x_45 = np.empty((len(im) ,len(im[0])))
    total_y_0 = np.empty((len(im) ,len(im[0])))
    total_y_225 = np.empty((len(im), len(im[0])))
    total_y_45 = np.empty((len(im), len(im[0])))
    indexes_0, indexes_225, indexes_45, indexes, indexes_0_y,  indexes_45_y,  indexes_225_y, indexes_y = [],[],[],[],[],[],[],[]
    total = np.zeros((len(im) ,len(im[0])))
    total_y = np.zeros((len(im) ,len(im[0])))
    lst_all_diameters, lst_all_diameters_x, lst_all_diameters_y = [],[],[]
    for y in range(len(im)):
            for x in range(len(im[y])):
                if im[y][x]==0:
                    # X-SCAN#
                    if im[y][x -1]==255:
                        # x0 scan
                        start_0_x = [x,y]
                        end_0_x = [x,y]
                        count_0_x = 1
                        for k in range(x + 1 ,len(im[y])):
                            if im[y][k] == 0:
                                end_0_x = [k,y]
                                count_0_x += 1
                            else:
                                total_x_0[y][x] = count_0_x * 500 / scale
                                indexes_0.append([start_0_x,end_0_x])
                                break
                        # x225 scan
                        count_225_x = 1
                        start_225_x = [x,y]
                        end_225_x = [x,y]
                        for k in range(x + 1 ,len(im[y])):
                            if im[y+count_225_x][k] == 0:
                                end_225_x = [k, y+count_225_x]
                                count_225_x += 1
                            else:
                                total_x_225[y][x] = count_225_x * 1.4141 * 500 / scale
                                indexes_225.append([start_225_x, end_225_x])
                                break
                        # x45 scan
                        count_45_x = 1
                        start_45_x = [x, y]
                        end_45_x = [x, y]
                        for k in range(x + 1, len(im[y])):
                            if im[y - count_45_x][k] == 0:
                                end_45_x = [k, y - count_45_x]
                                count_45_x += 1
                            else:
                                total_x_45[y][x] = count_45_x * 1.4141 * 500 / scale
                                indexes_45.append([start_45_x, end_45_x])
                                break

                        # find minimum on X
                        if ( total_x_225[y][x] <= total_x_0[y][x] ) and ( total_x_225[y][x] <= total_x_45[y][x] ) and ( total_x_225[y][x] > min_distance ):
                            total[y][x] = total_x_225[y][x]
                            indexes.append([start_225_x, end_225_x])
                            lst_all_diameters_x.append(total_x_225[y][x])
                            lst_all_diameters.append(total_x_225[y][x])
                        elif ( total_x_0[y][x] <= total_x_225[y][x] ) and ( total_x_0[y][x] <= total_x_45[y][x] ) and ( total_x_0[y][x] > min_distance ):
                            indexes.append([start_0_x, end_0_x])
                            total[y][x] = total_x_0[y][x]
                            lst_all_diameters_x.append(total_x_0[y][x])
                            lst_all_diameters.append(total_x_0[y][x])
                        elif (total_x_45[y][x] <= total_x_225[y][x]) and (total_x_45[y][x] <= total_x_0[y][x]) and ( total_x_45[y][x] > min_distance):
                            indexes.append([start_45_x, end_45_x])
                            total[y][x] = total_x_45[y][x]
                            lst_all_diameters_x.append(total_x_45[y][x])
                            lst_all_diameters.append(total_x_45[y][x])
                    #Y-SCAN#
                    if im[y-1][x] == 255:
                        # y0 scan
                        start_0_y = [x,y]
                        end_0_y = [x,y]
                        count_0_y = 1
                        for k in range(y + 1 ,len(im)):
                            if im[k][x] == 0:
                                end_0_y = [x,k]
                                count_0_y += 1
                            else:
                                total_y_0[y][x] = count_0_y * 500 / scale
                                indexes_0_y.append([start_0_y,end_0_y])
                                break
                        # y225 scan
                        count_225_y = 1
                        start_225_y = [x, y]
                        end_225_y = [x, y]
                        for k in range(y + 1,len(im)):
                            if im[k][x - count_225_y] == 0:
                                end_225_y = [x - count_225_y, k]
                                count_225_y += 1
                            else:
                                total_y_225[y][x] = count_225_y * 1.4141 * 500 / scale
                                indexes_225_y.append([start_225_y, end_225_y])
                                break
                        # y45 scan
                        count_45_y = 1
                        start_45_y = [x, y]
                        end_45_y = [x, y]
                        for k in range(y + 1,len(im)):
                            if im[k][x + count_45_y] == 0:
                                end_45_y = [x + count_45_y, k]
                                count_45_y += 1
                            else:
                                total_y_45[y][x] = count_45_y * 1.4141 * 500 / scale
                                indexes_45_y.append([start_45_y, end_45_y])
                                break
                        # find minimum on Y
                        if ( total_y_225[y][x] <= total_y_0[y][x] ) and ( total_y_225[y][x] <= total_y_45[y][x] ) and (total_y_225[y][x] > min_distance):
                            total_y[y][x] = total_y_225[y][x]
                            indexes_y.append([start_225_y, end_225_y])
                            lst_all_diameters_y.append(total_y_225[y][x])
                            lst_all_diameters.append(total_y_225[y][x])
                        elif ( total_y_0[y][x] <= total_y_225[y][x] ) and ( total_y_0[y][x] <= total_y_45[y][x] ) and (total_y_0[y][x] > min_distance):
                            indexes_y.append([start_0_y, end_0_y])
                            total[y][x] = total_y_0[y][x]
                            lst_all_diameters_y.append(total_y_0[y][x])
                            lst_all_diameters.append(total_y_0[y][x])
                        elif  ( total_y_45[y][x] <= total_y_225[y][x] ) and ( total_y_45[y][x] <= total_y_0[y][x] ) and (total_y_45[y][x] > min_distance):
                            indexes_y.append([start_45_y, end_45_y])
                            total_y[y][x] = total_y_45[y][x]
                            lst_all_diameters_y.append(total_y_45[y][x])
                            lst_all_diameters.append(total_y_45[y][x])
    t0_1 = time.time()
    total_det = t0_1 - t0
    selector0 = Label(newWindow, text="Done...", font='12').pack()
    newWindow.update()
    selector1 =Label(newWindow, text="Time needed for " + str(len(lst_all_diameters)) + " detections: " + str(round(total_det,2)) + " sec \n", font='12').pack()
    newWindow.update()
    selector2 = Label(newWindow, text="> Calculating diameter...",font = '12').pack()
    t1 = time.time()
    newWindow.update()
    avg = Average(lst_all_diameters)
    avg_x =  Average(lst_all_diameters_x)
    avg_y = Average(lst_all_diameters_y)
    print('The average diameter in x axis is: ', avg_x)
    print('The average diameter in y axis is: ', avg_y)
    print('The average diameter in both axes is: ', avg)
    t2 = time.time()
    total_calc = t2 - t1
    selector3 = Label(newWindow, text="Average cnt diameter is: " + str(round(avg,2)) + "nm", font='12').pack()
    newWindow.update()
    selector4 = Label(newWindow, text="Time needed for diameter calculation: " + str(round(total_calc,10)) + " sec \n", font='12').pack()
    newWindow.update()

    t3 = time.time()
    selector6 = Label(newWindow, text="> Plotting... ",                      font='12').pack()
    newWindow.update()

    print('Starting visualizations...')
    #X AXIS visualization#
    plt.imshow(im, cmap='gray')
    for item in indexes:
        x = [item[0][0],item[1][0]]
        y = [item[0][1],item[1][1]]
        plt.plot(x, y)
        plt.xticks([])
        plt.yticks([])
    plt.title("Detections with Image Processing \n Average diameter in nanometer: " + str(round(avg_x,2)))
    plt.draw()
    plt.pause(0.001)
    input("Press enter to continue...")
    plt.close()
    # Y AXIS visualization#
    plt.imshow(im, cmap='gray')
    for item in indexes_y:
        x = [item[0][0],item[1][0]]
        y = [item[0][1],item[1][1]]
        plt.plot(x, y)
        plt.xticks([])
        plt.yticks([])
    plt.title("Detections with Image Processing \n Average diameter in nanometer: " + str(round(avg_y,2)))
    plt.draw()
    plt.pause(0.001)
    input("Press enter to continue...")
    plt.close()
    plot_hist(lst_all_diameters)
Exemple #38
0
    def __init__(self,
                 rom_file,
                 viz=0,
                 frame_skip=4,
                 nullop_start=30,
                 live_lost_as_eoe=True,
                 max_num_frames=0):
        """
        Args:
            rom_file: path to the rom
            frame_skip: skip every k frames and repeat the action
            viz: visualization to be done.
                Set to 0 to disable.
                Set to a positive number to be the delay between frames to show.
                Set to a string to be a directory to store frames.
            nullop_start: start with random number of null ops.
            live_losts_as_eoe: consider lost of lives as end of episode. Useful for training.
            max_num_frames: maximum number of frames per episode.
        """
        super(AtariPlayer, self).__init__()
        if not os.path.isfile(rom_file) and '/' not in rom_file:
            rom_file = get_dataset_path('atari_rom', rom_file)
        assert os.path.isfile(rom_file), \
            "rom {} not found. Please download at {}".format(rom_file, ROM_URL)

        try:
            ALEInterface.setLoggerMode(ALEInterface.Logger.Error)
        except AttributeError:
            if execute_only_once():
                logger.warn("You're not using latest ALE")

        # avoid simulator bugs: https://github.com/mgbellemare/Arcade-Learning-Environment/issues/86
        with _ALE_LOCK:
            self.ale = ALEInterface()
            self.rng = get_rng(self)
            self.ale.setInt(b"random_seed", self.rng.randint(0, 30000))
            self.ale.setInt(b"max_num_frames_per_episode", max_num_frames)
            self.ale.setBool(b"showinfo", False)

            self.ale.setInt(b"frame_skip", 1)
            self.ale.setBool(b'color_averaging', False)
            # manual.pdf suggests otherwise.
            self.ale.setFloat(b'repeat_action_probability', 0.0)

            # viz setup
            if isinstance(viz, six.string_types):
                assert os.path.isdir(viz), viz
                self.ale.setString(b'record_screen_dir', viz)
                viz = 0
            if isinstance(viz, int):
                viz = float(viz)
            self.viz = viz
            if self.viz and isinstance(self.viz, float):
                self.windowname = os.path.basename(rom_file)
                cv2.startWindowThread()
                cv2.namedWindow(self.windowname)

            self.ale.loadROM(rom_file.encode('utf-8'))
        self.width, self.height = self.ale.getScreenDims()
        self.actions = self.ale.getMinimalActionSet()

        self.live_lost_as_eoe = live_lost_as_eoe
        self.frame_skip = frame_skip
        self.nullop_start = nullop_start

        self.action_space = spaces.Discrete(len(self.actions))
        self.observation_space = spaces.Box(low=0,
                                            high=255,
                                            shape=(self.height, self.width),
                                            dtype=np.uint8)
        self._restart_episode()
Exemple #39
0
def getit(_width,_height) :
	# grab the current frame
	camera = cv2.VideoCapture(0)
	(grabbed, frame) = camera.read()#parameter1-bool ; Pmtr2-array

 # resize the frame, blur it, and convert it to the HSV color space
	frame = imutils.resize(frame, width=_width,height=_height)
	frame = cv2.flip(frame,1)
	blurred = cv2.GaussianBlur(frame, (11, 11), 0)
	hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
	mask = cv2.inRange(hsv, greenLower, greenUpper)
	mask = cv2.erode(mask, None, iterations=2)  #clean up data
	mask = cv2.dilate(mask, None, iterations=2) #^
	# find contours in the mask and initialize the current (x, y) center of the color mass
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, #Finds blobs of
		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
		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)

	# update the points queue
	pts.appendleft(center)
	# loop over the set of tracked points
	for i in xrange(1, len(pts)):
		# if either of the tracked points are None, ignore
		# them
		if pts[i - 1] is None or pts[i] is None:
			continue
		cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), 4)
	key = cv2.waitKey(1) & 0xFF
	if key == ord("q"):
		return 0
	camera.release()
	#return(frame,center)

	# show the frame to our screen
	cv2.namedWindow("base-image", cv2.WINDOW_AUTOSIZE)
	cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE)

	#Position the windows next to eachother
	cv2.moveWindow("base-image",0,100)
	cv2.moveWindow("result-image",600,100)

	#Start the window thread for the two windows we are using
	cv2.startWindowThread()

	cv2.imshow("base-image", frame)

	cv2.imshow("result-image", mask)
Exemple #40
0
def detectAndTrackMultipleFaces():
    cam = 'test_out_04.avi'
    capture = cv2.VideoCapture(cam)
    process_this_frame = True


    cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE)

    cv2.moveWindow("result-image", 400, 100)

    cv2.startWindowThread()

    rectangleColor = (0,165,255)

    frameCounter = 0
    currentFaceID = 0

    faceTrackers = {}
    faceNames = {}

    try:
        while True:
            rc,fullSizeBaseImage = capture.read()

            baseImage = cv2.resize(fullSizeBaseImage, (0,0), fx = 0.6, fy = 0.6)
            baseImage = baseImage[:, :, ::-1]

            pressedKey = cv2.waitKey(5)
            if pressedKey == ord('Q'):
                break



   
            resultImage = baseImage.copy()




            frameCounter += 1



            fidsToDelete = []
            for fid in faceTrackers.keys():
                trackingQuality = faceTrackers[ fid ].update( baseImage )


                if trackingQuality < 7:
                    fidsToDelete.append( fid )

            for fid in fidsToDelete:
                print("Removing fid " + str(fid) + " from list of trackers")
                faceTrackers.pop( fid , None )




                #best param = 6
            if (frameCounter % 6) == 0:




                gray = cv2.cvtColor(baseImage, cv2.COLOR_BGR2GRAY)


                
                
                # y = top
                # x = left
                # h = bottom - y
                # w = right - x 



                
                
                # face_locations = face_recognition.face_locations(baseImage)
                face_locations = faceCascade.detectMultiScale(gray, 1.3, 5)
                # top, right, bottom, left

                

                fl = []
                qwe = 1
                for (_x,_y,_w,_h) in face_locations:
                    if (_w**2 + _h**2)**0.5 < 100:
                        fl.append((_x,_y,_w,_h))
                face_locations = fl
                del fl

                face_locations = [(_y, _x+_w, _y+_h, _x) for (_x,_y,_w,_h) in face_locations]

                face_encodings = face_recognition.face_encodings(baseImage, face_locations)
                
                face_names = []
                for face_encoding in face_encodings:

                    matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                    name = "Unknown"

                    if True in matches:
                        first_match_index = matches.index(True)
                        name = known_face_names[first_match_index]
                        
                        
                    face_names.append(name)

            




                for (top, right, bottom, left), name in zip(face_locations, face_names):
                    y = top
                    x = left
                    h = bottom - y
                    w = right - x 




                    x_bar = x + 0.5 * w
                    y_bar = y + 0.5 * h



 
                    matchedFid = None

       
                    for fid in faceTrackers.keys():
                        tracked_position =  faceTrackers[fid].get_position()

                        t_x = int(tracked_position.left())
                        t_y = int(tracked_position.top())
                        t_w = int(tracked_position.width())
                        t_h = int(tracked_position.height())
                        

                        

                        #Считаем центр
                        t_x_bar = t_x + 0.5 * t_w
                        t_y_bar = t_y + 0.5 * t_h

      
                        if ( ( t_x <= x_bar   <= (t_x + t_w)) and
                             ( t_y <= y_bar   <= (t_y + t_h)) and
                             ( x   <= t_x_bar <= (x   + w  )) and
                             ( y   <= t_y_bar <= (y   + h  ))):
                            matchedFid = fid

                                        # Если нет трека, делаем новый
                    if matchedFid is None:
                  

                        print("Creating new tracker " + str(currentFaceID))

                        #Create and store the tracker
                        tracker = dlib.correlation_tracker()
                        tracker.start_track(baseImage,
                                            dlib.rectangle( x-10,
                                                            y-20,
                                                            x+w+10,
                                                            y+h+20))

                        faceTrackers[currentFaceID] = tracker

                        
                        faceNames[currentFaceID] = name
                        alarm_bool = (name == 'Unknown')
                        
                        if name != 'Unknown':
                            status_type = 'student'
                        else:
                            status_type = 'Unknown' 
                        
                        act = {'status':status_type,'name':name, 'alarm':str(alarm_bool)}
                        database.append_action(cam, act)

                        # Счетчик idшников
                        currentFaceID += 1





            for fid in faceTrackers.keys():
                tracked_position =  faceTrackers[fid].get_position()

                t_x = int(tracked_position.left())
                t_y = int(tracked_position.top())
                t_w = int(tracked_position.width())
                t_h = int(tracked_position.height())


                top = t_y
                bottom = t_y + t_h
                right = t_x + t_w
                left = t_x


                
                # cv2.rectangle(resultImage, (t_x, t_y),
                #                         (t_x + t_w , t_y + t_h),
                #                         (0,0,255) ,2)
                print(faceNames)

                cv2.rectangle(resultImage, (left, top), (right, bottom), (0, 0, 255), 2)

                cv2.rectangle(resultImage, (left, bottom - 10), (right, bottom), (0, 0, 255), cv2.FILLED)
                font = cv2.FONT_HERSHEY_DUPLEX
        # cv2.putText(image_full, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)


                try:

                    cv2.putText(resultImage, faceNames[fid], (left + 2, bottom - 1), font, 0.4, (255, 255, 255), 1)
                except KeyError:
                                cv2.putText(resultImage, faceNames[fid], (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)




            resultImage = resultImage[:, :, ::-1]
            # largeResult = cv2.resize(resultImage,
            #                          (OUTPUT_SIZE_WIDTH,OUTPUT_SIZE_HEIGHT))

            # Рисуем
            cv2.imshow("result-image", resultImage)







    except KeyboardInterrupt as e:
        pass

    cv2.destroyAllWindows()
    exit(0)
Exemple #41
0
def detectAndTrackMultipleFaces(model, imagesize):
    #Open the first webcame device
    capture = cv2.VideoCapture(0)

    #Create two opencv named windows
    # cv2.namedWindow("base-image", cv2.WINDOW_AUTOSIZE)
    cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE)

    #Position the windows next to eachother
    # cv2.moveWindow("base-image", 0, 100)
    cv2.moveWindow("result-image", 400, 100)

    #Start the window thread for the two windows we are using
    cv2.startWindowThread()

    #The color of the rectangle we draw around the face
    rectangleColor = (0, 165, 25)

    #variables holding the current frame number and the current faceid
    frameCounter = 0
    currentFaceID = 0
    flag_to_write_csv = 0
    result_age = []
    #Variables holding the correlation trackers and the name per faceid
    faceTrackers = {}
    faceNames = {}
    try:
        time_start = time.time()

        while True:
            #Retrieve the latest image from the webcam
            rc, fullSizeBaseImage = capture.read()

            #Resize the image to 320x240
            fullSizeBaseImage = cv2.flip(fullSizeBaseImage, 1)
            baseImage = fullSizeBaseImage
            # baseImage = cv2.resize(fullSizeBaseImage, (1400, 1400));

            #Check if a key was pressed and if it was Q, then break
            #from the infinite loop
            pressedKey = cv2.waitKey(2)

            if pressedKey == ord('Q'):
                break

            #Result image is the image we will show the user, which is a
            #combination of the original image from the webcam and the
            #overlayed rectangle for the largest face
            resultImage = baseImage.copy()

            #Increase the framecounter
            frameCounter += 1

            #Update all the trackers and remove the onges for which the update
            #indicated the quality was not good enough
            fidsToDelete = []
            for fid in faceTrackers.keys():
                trackingQuality = faceTrackers[fid].update(baseImage)
                #If the tracking quality is good enough, we must delete
                if trackingQuality < 10:
                    fidsToDelete.append(fid)
            num_person = len(faceTrackers) - len(fidsToDelete)
            for fid in fidsToDelete:
                faceTrackers.pop(fid, None)

            #Every 10 frames, we will have to determine which faces
            #are present in the frame
            if (frameCounter % 10) == 0:

                gray = cv2.cvtColor(baseImage, cv2.COLOR_BGR2RGB)
                faces = hog_face_detector(gray, 1)
                for i, d in enumerate(faces):

                    x, y, x2, y2, w, h = d.left(), d.top(), d.right() + 1,\
                                           d.bottom() + 1, d.width(), d.height()

                    #calculate the centerpoint
                    x_bar = x + 0.5 * w
                    y_bar = y + 0.5 * h

                    matchedFid = None

                    #Now loop over all the trackers and check if the
                    #centerpoint of the face is within the box of a tracker
                    for fid in faceTrackers.keys():
                        tracked_position = faceTrackers[fid].get_position()

                        t_x = int(tracked_position.left())
                        t_y = int(tracked_position.top())
                        t_w = int(tracked_position.width())
                        t_h = int(tracked_position.height())

                        #calculate th   e centerpoint
                        t_x_bar = t_x + 0.5 * t_w
                        t_y_bar = t_y + 0.5 * t_h

                        #check if the centerpoint of the face is within the
                        #rectangleof a tracker region. Also, the centerpoint
                        #of the tracker region must be within the region
                        #detected as a face. If both of these conditions hold
                        #we have a match
                        if ((t_x <= x_bar <= (t_x + t_w)) and (t_y <= y_bar <=
                                                               (t_y + t_h))
                                and (x <= t_x_bar <=
                                     (x + w)) and (y <= t_y_bar <= (y + h))):
                            matchedFid = fid

                    #If no matched fid, then we have to create a new tracker
                    if matchedFid is None:
                        # print("Creating new tracker " + str(currentFceID))

                        #Create and store the tracker
                        tracker = dlib.correlation_tracker()
                        tracker.start_track(baseImage,
                                            dlib.rectangle(x, y, x + w, y + h))

                        faceTrackers[currentFaceID] = tracker

                        #Start a new thread that is used to simulate
                        #face recognition. This is not yet implemented in this
                        #version :)
                        t = threading.Thread(target=doRecognizePerson,
                                             args=(faceNames, result_age,
                                                   currentFaceID))
                        t.start()

                        #Increase the currentFaceID counter
                        currentFaceID += 1

            #tracking
            time_wirte_csv = time.time()
            if time_wirte_csv - time_start > 3:
                flag_to_write_csv = 1
            for fid in faceTrackers.keys():
                tracked_position = faceTrackers[fid].get_position()
                t_x = int(tracked_position.left())
                t_y = int(tracked_position.top())
                t_w = int(tracked_position.width())
                t_h = int(tracked_position.height())
                image = baseImage[t_y - int(t_h / 2):t_y + t_h - 5,
                                  t_x - 5:t_x + t_w + 5, :]
                try:
                    # if flag_to_write_csv:
                    cv2.imshow("image", image)
                    gender, age = predict(model, image, imagesize)
                except:
                    continue

                cv2.rectangle(resultImage, (t_x, t_y), (t_x + t_w, t_y + t_h),
                              rectangleColor, 2)

                if fid in faceNames.keys():
                    result_age[fid].append(age)
                    if len(result_age[fid]) > 10:
                        age = Counter(
                            result_age[fid][-10:-1]).most_common()[0][0]

                    if flag_to_write_csv:
                        # print(Counter(result_age[fid]))
                        # print(result_age)
                        path_out = "output" + "/{}.csv".format(date.today())
                        if not os.path.exists(path_out):
                            gen_csv(path_out)
                        save_csv(gender, age, path_out)

                    cv2.putText(
                        resultImage, " " + label_gender[str(gender)] +
                        ",{}".format(label_age[str(age)]),
                        (int(t_x + t_w / 5), int(t_y)),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1)

                else:
                    cv2.putText(resultImage, "Detecting...",
                                (int(t_x + t_w / 2), int(t_y)),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255),
                                2)

            if flag_to_write_csv:
                flag_to_write_csv = 0
                time_start = time.time()

            largeResult = cv2.resize(resultImage,
                                     (OUTPUT_SIZE_WIDTH, OUTPUT_SIZE_HEIGHT))

            #Finally, we want to show the images on the screen
            # cv2.imshow("base-image", baseImage)
            cv2.imshow("result-image", largeResult)

    except KeyboardInterrupt as e:
        pass

    #Destroy any OpenCV windows and exit the application
    cv2.destroyAllWindows()
    exit(0)
    def track(self):
        #print("inside track")
        count = 0
        global temp
        cap = cv2.VideoCapture(0)

        # Start the window thread for the two windows we are using
        cv2.startWindowThread()

        # The color of the rectangle we draw around the face
        rectangleColor = (0, 165, 255)

        # variables holding the current frame number and the current faceid
        frameCounter, currentFaceID = 0, 0
        # Variables holding the correlation trackers and the name per faceid
        faceTrackers, faceNames, face_locations_len_previous, outputs_len_previous = {}, {}, 0, 0
        while True:
            
            tk = []
            frameCounter += 1
            global c3
            global c4
            full = timer()
            ret, frame = cap.read()
            gray_img=cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
            face_locations=faceCascade.detectMultiScale(gray_img, scaleFactor=1.3,minNeighbors=4,minSize=(40,40))
            frame = cv2.flip(frame, 1)
            frame = cv2.resize(frame, (1080, 1080))
            gray_img =cv2.flip(frame, 1)
            gray_img = cv2.resize(frame, (1080, 1080))
            face_locations_haar=faceCascade.detectMultiScale(gray_img, scaleFactor=1.3,minNeighbors=4,minSize=(40,40))
            face_locations = change_coor(face_locations_haar)
            start = timer()
            resultImg,faceBoxes=highlightFace(faceNet,frame)
           # print("face boxes retrieve karwaama  ",timer()-start)
            
            half = timer()
            bbox_xywh, cls_conf, cls_ids = self.detector(frame)

            if (bbox_xywh is not None) & (frameCounter%2 ==0) :
                count, start = 0, timer()
                mask = cls_ids == 0
                bbox_xywh[:, 3:] *= 1.2
                c1 = 0
                c2 = 0

                
                


                cls_conf = cls_conf[mask]
                
                outputs = self.deepsort.update(bbox_xywh, cls_conf, frame)
                outputs_length = len(outputs)
                #face_locations = face_R(frame)
                #print("FUNCTION maa atli waar laagi ", timer()-start)
                
                #face_locations_length = len(face_locations)
                face_locations_length=len(face_locations)
                print("Total no of person: ",face_locations_length)
                #asyncio.get_event_loop().run_until_complete(command_receiver(face_locations_length))
                for top, right, bottom, left in face_locations:
                    cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
                for fc in faceBoxes:
                    face=frame[max(0,fc[1]-padding):
                               min(fc[3]+padding,frame.shape[0]-1),max(0,fc[0]-padding)
                               :min(fc[2]+padding, frame.shape[1]-1)]
                    blob=cv2.dnn.blobFromImage(face, 1.0, (227,227), MODEL_MEAN_VALUES, swapRB=False)
                    genderNet.setInput(blob)
                    genderPreds=genderNet.forward()
                    gender=genderList[genderPreds[0].argmax()]
                    #print(f'Gender: {gender}')
##                    ageNet.setInput(blob)
##                    agePreds=ageNet.forward()
##                    age=ageList[agePreds[0].argmax()]
##                    print(f'Age: {age[1:-1]} years')

                    if gender=='Male':
                        #print("Male detected")
                        c1 = c1+1
                    elif gender=='Female':
                        #print("Female detected")
                        c2 = c2 +1
                    else:
                        gender = '0'
                    for top, right, bottom, left in face_locations:
                        [x,y] = centre(top,right,bottom,left)
                       # temp2.append([x,y])  #list of centres for gender
                    
                        if [x,y] == [0,0] :
                            [x,y] = 'NA'
                        else :
                            info = [[x,y],gender]
                            tk.append(info)
                            #print("info",tk)
                            
                            print('Number of Male detected',c1)
                            print('Number of Female detected',c2)
    
                    
                temp1 = []
                
                if outputs_length >= outputs_len_previous:
                    for i in range(len(face_locations)):
                        t1, t2, t3, t4 = face_locations[i]
                        temp = centre(t1, t2, t3, t4)
                        temp1.append(temp)
                    #print("Centre Coordinmates",temp1)
                
                for i in range(len(outputs)):
                    if outputs[i][4] not in total_p:
                        total_p.append(outputs[i][4])

                
                if len(outputs) > 0:
                    bbox_xyxy = outputs[:, :-1]
                    identities = outputs[:, -1]
                    frame = draw_boxes(frame, bbox_xyxy, identities)
                start = timer()
                if outputs_length >= outputs_len_previous:
                    #print("yes,andar aavyo")
                    for i in range(len(outputs)):
                        if (outputs[i][4] not in nv):
                            #print("che toh nai")
                            for j in range(len(temp1)):
                                a, b, c, d, e = outputs[i]
                                flag = FindPoint(a, b, c, d, temp1[j][0], temp1[j][1])
                                if flag == True:
                                    if e not in nv:
                                        #print("navu print karyu")
                                        nv.append(e)
                                       # time.sleep(0.5)
                                        func(temp1[j][0],temp1[j][1],outputs_length,outputs_len_previous,faceBoxes,frame,tk)
##                                        #print("Answer",gender)
                                        if gender=='Male':
                                            c3 = c3+1
                                        elif gender=='Female':
                                           c4 = c4 +1
                                        else:
                                            pass
                                            
                                            
              #  print("Add karwaama ", timer()-start)
      
##                    print("total maa atlo time", timer()-full)      
                asyncio.get_event_loop().run_until_complete(command_receiver(outputs_length,face_locations_length,c1,c2,0,0,0,0))
                outputs_len_previous = outputs_length
            cv2.imshow("frame", frame)            
            if cv2.waitKey(1) & 0xFF == ord('q'):
                asyncio.get_event_loop().run_until_complete(command_receiver(outputs_length,face_locations_length,c1,c2,len(total_p),len(nv),c3,c4))
                break
            
        cap.release()
        cv2.destroyAllWindows()
        return [nv,total_p,c3,c4]
Exemple #43
0
def show_cv2_img_mac(): 
    cv2.startWindowThread()
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.waitKey(1)
def vidRoll(color):

    #needed to calculate and test out different values for bgr, such that they
    #match the different colors.
    lower_blue = np.array([110, 50, 50])
    upper_blue = np.array([130, 255, 255])
    lower_red = np.array([25, 25, 150])
    upper_red = np.array([50, 56, 255])
    lower_green = np.array([33, 80, 40])
    upper_green = np.array([102, 255, 255])
    lower_yellow = np.array([20, 100, 100])
    upper_yellow = np.array([30, 255, 255])

    startFrame = 50
    pointsList = []
    dframes = 0
    bpm = 0
    cap = cv2.VideoCapture(0)
    startTime = time.time()
    frameNumber = 0  #starts drawing when it hits this number
    changedList = []
    # while time.time()-startTime<30:
    #while True:
    rollIt = True
    cv2.startWindowThread()
    while True:
        # Take each frame
        _, frame = cap.read()
        frameNumber += 1  #increments frame number in order to keep track of time

        # Convert BGR to HSV
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        imgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        ret, thresh = cv2.threshold(imgray, 127, 255, 0)

        # Threshold the HSV image to get only blue colors
        if color == 'blue':
            mask = cv2.inRange(hsv, lower_blue, upper_blue)
        if color == 'yellow':
            mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
        if color == 'red':
            mask = cv2.inRange(hsv, lower_red, upper_red)
        if color == 'green':
            mask = cv2.inRange(hsv, lower_green, upper_green)
        #remove background noise
        mask = cv2.erode(mask, None, iterations=6)  #get rid of all blosb
        mask = cv2.dilate(mask, None,
                          iterations=6)  #increase the size back up to original

        #find contours can only operate on
        currContour = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)[-2]
        #second to last part of find contours
        center = None

        if len(currContour) > 0:
            c = max(currContour, key=cv2.contourArea)
            ((x, y), radius) = cv2.minEnclosingCircle(c)
            if radius > 30:
                center = (int(x), int(y))
                cv2.circle(frame, center, int(radius), (0, 255, 0), 2)
                thickness = int(radius // 4)

        # Bitwise-AND mask and original image
        res = cv2.bitwise_and(frame, frame, mask=mask)
        if center != None:
            if frameNumber > startFrame and len(pointsList) == 0:
                pointsList.append(center)
            elif len(pointsList) > 0:
                diffX = abs(pointsList[-1][0] - center[0])
                diffY = abs(pointsList[-1][1] - center[1])
                if (5 < diffX < 400 and 5 < diffY < 400):
                    pointsList.append(center)

        for i in range(len(pointsList) - 1):
            cv2.line(frame, pointsList[i], pointsList[i + 1], (255, 0, 0), 2)

        if len(pointsList) > 50:
            pointsList = pointsList[1:]

        directionChange = 0
        try:
            if alternateChangeDirection(pointsList):
                #print("changed direction%s"%(directionChange))
                directionChange += 1
                changedList.append(time.time() - startTime)
        except:
            pass
        if len(changedList) > 0 and frameNumber % 20 == 0:
            bpm = calculatePace(changedList)

        if (len(changedList) > 100):
            changedList = changedList[1:]
        if frameNumber <= startFrame:
            cv2.putText(frame, "Wait to start running", (0, 100),
                        cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255))
            cv2.putText(frame, "Get ready by placing colored object in frame",
                        (0, 150), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255))

        # if frameNumber >startFrame and bpm == 0:
        #     cv2.putText(frame, "Start Running!", (0, 100), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255))
        #     counter = time.time()-startTime
        #     cv2.putText(frame, "countdown: %.2f"%counter, (0, 150), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255))

        if bpm != 0:
            cv2.putText(frame, "Current Beats per Minute: %s" % (bpm),
                        (0, 100), cv2.FONT_HERSHEY_PLAIN, 2, (238, 130, 238))
        cv2.imshow('frame', frame)
        # cv2.imshow('mask',mask)
        # cv2.imshow('res',res)
        waitTime = 5
        if cv2.waitKey(waitTime) & 0xFF == ord('q'):
            break
    print('quit')
    cap.release()
    cv2.destroyAllWindows()
    return bpm
def main():
    """
    Load the network and parse the output.
    :return: None
    """
    global INFO
    global DELAY
    global POSE_CHECKED
    controller = MouseController()

    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = args_parser().parse_args()
    logger = log.getLogger()

    if args.input == 'cam':
        input_stream = 0
    else:
        input_stream = args.input
        assert os.path.isfile(args.input), "Specified input file doesn't exist"

    cap = cv2.VideoCapture(input_stream)
    initial_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    initial_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    video_len = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    out = cv2.VideoWriter(os.path.join(args.output_dir, "shopper.mp4"),
                          cv2.VideoWriter_fourcc(*"MP4V"), fps,
                          (initial_width, initial_height), True)
    frame_count = 0

    job_id = 1
    progress_file_path = os.path.join(args.output_dir,
                                      'i_progress_' + str(job_id) + '.txt')

    infer_time_start = time.time()

    if input_stream:
        cap.open(args.input)
        # Adjust DELAY to match the number of FPS of the video file
        DELAY = 1000 / cap.get(cv2.CAP_PROP_FPS)

    if not cap.isOpened():
        logger.error("ERROR! Unable to open video source")
        return

    # Initialise the class
    if args.cpu_extension:
        facedet = FaceDetection(args.facemodel,
                                args.confidence,
                                extensions=args.cpu_extension)
        posest = HeadPoseEstimation(args.posemodel,
                                    args.confidence,
                                    extensions=args.cpu_extension)
        landest = FaceLandmarksDetection(args.landmarksmodel,
                                         args.confidence,
                                         extensions=args.cpu_extension)
        gazeest = GazeEstimation(args.gazemodel,
                                 args.confidence,
                                 extensions=args.cpu_extension)
    else:
        facedet = FaceDetection(args.facemodel, args.confidence)
        posest = HeadPoseEstimation(args.posemodel, args.confidence)
        landest = FaceLandmarksDetection(args.landmarksmodel, args.confidence)
        gazeest = GazeEstimation(args.gazemodel, args.confidence)

    # Load the network to IE plugin to get shape of input layer

    facedet.load_model()
    posest.load_model()
    landest.load_model()
    gazeest.load_model()
    print("loaded models")
    ret, frame = cap.read()
    while ret:
        looking = 0
        POSE_CHECKED = False
        ret, frame = cap.read()
        frame_count += 1
        if not ret:
            print("checkpoint *BREAKING")
            break

        if frame is None:
            log.error("checkpoint ERROR! blank FRAME grabbed")
            break

        initial_width = int(cap.get(3))
        initial_height = int(cap.get(4))

        # Start asynchronous inference for specified request
        inf_start_fd = time.time()
        # Results of the output layer of the network
        coords, frame = facedet.predict(frame)
        if args.visualization == "fm":
            cv2.startWindowThread()
            cv2.namedWindow("preview")
            cv2.imshow("preview", frame)
        det_time_fd = time.time() - inf_start_fd
        if len(coords) > 0:
            [xmin, ymin, xmax,
             ymax] = coords[0]  # use only the first detected face
            head_pose = frame[ymin:ymax, xmin:xmax]
            inf_start_hp = time.time()
            is_looking, pose_angles = posest.predict(head_pose)
            if args.visualization == "pm":
                cv2.startWindowThread()
                cv2.namedWindow("preview")
                p = "Pose Angles {}, is Looking? {}".format(
                    pose_angles, is_looking)
                cv2.putText(frame, p, (50, 15), cv2.FONT_HERSHEY_COMPLEX, 0.5,
                            (255, 255, 255), 1)
                cv2.imshow("preview", frame)

            if is_looking:
                det_time_hp = time.time() - inf_start_hp
                POSE_CHECKED = True
                print(is_looking)
                inf_start_lm = time.time()
                coords, f = landest.predict(head_pose)
                if args.visualization == "lm":
                    cv2.startWindowThread()
                    cv2.namedWindow("preview")
                    cv2.imshow("preview", f)

                frame[ymin:ymax, xmin:xmax] = f
                det_time_lm = time.time() - inf_start_lm
                [[xlmin, ylmin, xlmax, ylmax], [xrmin, yrmin, xrmax,
                                                yrmax]] = coords
                left_eye_image = f[ylmin:ylmax, xlmin:xlmax]
                right_eye_image = f[yrmin:yrmax, xrmin:xrmax]
                output, gaze_vector = gazeest.predict(left_eye_image,
                                                      right_eye_image,
                                                      pose_angles)
                if args.visualization == "gm":
                    cv2.startWindowThread()
                    cv2.namedWindow("preview")
                    p = "Gaze Vector {}".format(gaze_vector)
                    cv2.putText(frame, p, (50, 15), cv2.FONT_HERSHEY_COMPLEX,
                                0.5, (255, 255, 255), 1)
                    fl = draw_gaze(left_eye_image, gaze_vector)
                    fr = draw_gaze(right_eye_image, gaze_vector)
                    f[ylmin:ylmax, xlmin:xlmax] = fl
                    f[yrmin:yrmax, xrmin:xrmax] = fr
                    cv2.imshow("preview", f)

                if frame_count % 10 == 0:
                    controller.move(output[0], output[1])
        # Draw performance stats
        inf_time_message = "Face Inference time: {:.3f} ms.".format(
            det_time_fd * 1000)
        #
        if POSE_CHECKED:
            cv2.putText(
                frame, "Head pose Inference time: {:.3f} ms.".format(
                    det_time_hp * 1000), (0, 35), cv2.FONT_HERSHEY_SIMPLEX,
                0.5, (255, 255, 255), 1)
            cv2.putText(frame, inf_time_message, (0, 15),
                        cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
        out.write(frame)
        if frame_count % 10 == 0:
            demoutils.progressUpdate(progress_file_path,
                                     int(time.time() - infer_time_start),
                                     frame_count, video_len)
        if args.output_dir:
            total_time = time.time() - infer_time_start
            with open(os.path.join(args.output_dir, 'stats.txt'), 'w') as f:
                f.write(str(round(total_time, 1)) + '\n')
                f.write(str(frame_count) + '\n')
    facedet.clean()
    posest.clean()
    landest.clean()
    gazeest.clean()
    cap.release()
    cv2.destroyAllWindows()
    out.release()
Exemple #46
0
 def _initialize_window(self):
     cv2.namedWindow("template", cv2.WINDOW_NORMAL)
     cv2.resizeWindow('template', 1280, 720)
     cv2.startWindowThread()
Exemple #47
0
"""
Created on Mon Feb 18 21:02:03 2019

@author: student
"""
from cv2 import namedWindow, imread, imshow
from cv2 import waitKey, destroyAllWindows, startWindowThread
from cv2 import blur, Canny, circle

# declare windows you want to display
namedWindow("original")
namedWindow("blur")
namedWindow("canny")

# this is always needed to run the GUI thread
startWindowThread()

img = imread('../blofeld.jpg')
imshow("original", img)
# create a new blurred image:
img2 = blur(img, (7, 7))
# draw on the image:
circle(img2, (100, 100), 30, (255, 0, 255), 5)
# display the image:
imshow("blur", img2)
# Canny is an algorith for edge detection
img3 = Canny(img, 10, 200)
imshow("canny", img3)
# the shape gives you the dimensions
h = img3.shape[0]
w = img3.shape[1]
Exemple #48
0
def main_tmp():
    global mouse_button_clicked, selected_centroid
    #patient id 192 has a problematic xml file, so it's removed for now
    #patient_id_lst = [68,71,72,88,90,91,100,118,124,129,135,137,138,143,149,159,161,162,163,164,165,166,
    #                  167,168,169,171,173,174,175,176,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
    #                  193,194,197,198,200,202,203,205,207,210,211,212,213,214,217,220,221,222,223,224,225,
    #                  226,230,231,232,233,234,235,236,237,239,242,243,244,245,246,247,248,249,250,251,252,253,
    #                  254,255,256,257,258,260,261,264,265,266,267,268,270,271,272,273,274,275,276,277,278,279,
    #                  280,281,282,283,285,286,287,288,289,290,314,325,332,377,385,399,405,454,470,493,510,522,543,
    #                  559,562,568,568,576,580,610,624,766,771,772,811,818,875,921,924,939,965,994,1002,1004]
    #patient_id_lst = [332,377,385,399,470,493,559,568,580,610,772,818,939,994,1004]
    #patient_id_lst = [162,165,190,203,248,470]
    #patient_id_lst = [165,176,178,180,198,202,231,243,266,285,818]
    #patient_id_lst = [168,198,200,202,203,212,230,231,245,248,256,270,273,282,283,286,287,288,399,559,580,818]
    gt_save_root_path = "C:/Users/tizita/Desktop/CODE/Lung-msc-code/gt/"
    patient_id_lst = [72]
    for patient_id in patient_id_lst:
        print "\n\n Processing patient id %d" % patient_id

        xml_dt = LIDCXmlParser(
            ('C:/Users/tizita/Desktop/CODE/Lung-msc-code/xml_data/%04d.xml') %
            patient_id)  #trial-xml.xml')
        xml_dt.parse()
        print xml_dt.xml_header
        patient_ct_series = PatientCTSeries(
            dt_root_path="C:/Users/tizita/Documents/LIDC/LIDC-IDRI/",
            patient_no=patient_id)
        patient_ct_series.populate_from_xmlparser(xml_dt)

        cv2.namedWindow("LIDC-IDRI")
        cv2.setMouseCallback("LIDC-IDRI", mouseClickCallback)
        cv2.startWindowThread()  #important

        for trial in [1, 2]:
            print "Round : ", trial

            mouse_button_clicked = False
            selected_centroid = []
            for ct in patient_ct_series.annotated_cts:

                if ct.no_consensus_annots() > 0:  #if one or more rads agree
                    clusters = ct.get_nodule_clusters()
                    cv_mat = ct.draw(rad_indx_lst=range(
                        len(xml_dt.rad_annotations)),
                                     draw_nodules=True,
                                     draw_small=False,
                                     draw_non=False)
                    for cluster in clusters:
                        cvx_hull = cluster.convex_hull_with_margin
                        #cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])#
                        cv2.rectangle(img=cv_mat,
                                      pt1=cvx_hull[0],
                                      pt2=cvx_hull[3],
                                      color=(0, 255, 0),
                                      thickness=1)

                    cv2.imshow('LIDC-IDRI', cv_mat)
                    ky = cv2.waitKey(0)
                    if ky == 27:  #Esc key
                        cv2.waitKey(1)
                        cv2.destroyAllWindows()
                        #cv2.waitKey(1)
                        break

            if (mouse_button_clicked):
                #create a directory in the root path

                dst_path = "%s/gt%04d/" % (gt_save_root_path, patient_id)
                if not os.path.exists(dst_path):
                    os.makedirs(dst_path)

                fo = open("%s/centroid.txt" % dst_path, "w")
                fo.write("%d\n" % len(selected_centroid))
                for (x, y) in selected_centroid:
                    fo.write("%d,%d\n" % (x, y))

                fo.close()

                print "[%d] cluster centroid(s) defined...processing data.." % len(
                    selected_centroid)
                for indx, ct in enumerate(patient_ct_series.annotated_cts):

                    if ct.no_consensus_annots() > 0:
                        ct.save_nearest_cluster(selected_centroid, indx,
                                                dst_path)

        print "Finished processing patient id %d" % patient_id
Exemple #49
0
def camera_recognition(test_folder_path='../default_picture_labels'):
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(
        '../resources/shape_predictor_68_face_landmarks.dat')

    # 创建cv2摄像头对象
    cap = cv2.VideoCapture(0)

    # cap.set(proID,value)
    # 设置视频参数,propId设置的视频参数,value设置的参数值
    cap.set(3, 480)

    # 截图screenshoot的计数器
    cnt_ss = 0

    # 人脸截图的计数器
    cnt_p = 0

    # 保存
    path_save = r"../camera_photo/"

    # cap.isOpened() 返回true/false检查初始化是否成功
    while cap.isOpened():
        # cap.read()
        # 返回两个值:
        #    一个布尔值true/false,用来判断读取视频是否成功/是否到视频末尾
        #    图像对象,图像的三维矩阵q
        flag, im_rd = cap.read()

        # 每帧数据延时1ms,延时为0读取的是静态帧
        kk = cv2.waitKey(1)

        # 取灰度
        img_gray = cv2.cvtColor(im_rd, cv2.COLOR_RGB2GRAY)

        # 人脸数rects
        rects = detector(img_gray, 0)

        # print(len(rects))

        # 设置接下来的字体
        font = cv2.FONT_HERSHEY_SIMPLEX

        if len(rects) != 0:
            # 检测到人脸
            # 第一个参数 ret 的值为 True 或 False,代表有没有读到图片。第二个参数是 frame,是当前截取一帧的图片
            ret, frame = cap.read()
            if kk == ord('s'):
                cnt_p += 1
                cv2.imwrite(path_save + "img_face_" + str(cnt_p) + ".jpg",
                            frame)
                print("Write to local:",
                      path_save + "img_face_" + str(cnt_p) + ".jpg")
            # 矩形框
            for k, d in enumerate(rects):
                # 计算矩形大小
                # pos_start = tuple([d.left(), d.top()])
                # pos_end = tuple([d.right(), d.bottom()])

                # 计算矩形框大小
                height = d.bottom() - d.top()
                width = d.right() - d.left()

                # 根据人脸大小生成空的图像
                cv2.rectangle(im_rd, tuple([d.left(), d.top()]),
                              tuple([d.right(), d.bottom()]), (0, 255, 255), 2)
                # im_blank = np.zeros((height, width, 3), np.uint8)
            #
            #     # 按下's'保存摄像头中的人脸到本地
            #     if kk == ord('s'):
            #         cnt_p += 1
            #         for ii in range(height):
            #             for jj in range(width):
            #                 im_blank[ii][jj] = im_rd[d.top() + ii][d.left() + jj]
            #         # 存储人脸图像文件
            #         cv2.imwrite(path_save + "img_face_" + str(cnt_p) + ".jpg", im_blank)
            #         print("写入本地:", path_save + "img_face_" + str(cnt_p) + ".jpg")

            # 显示人脸数
            cv2.putText(im_rd, "faces:" + str(len(rects)), (20, 50), font, 1,
                        (0, 0, 255), 1, cv2.LINE_AA)

        else:
            # 没有检测到人脸
            cv2.putText(im_rd, "no face", (20, 50), font, 1, (0, 0, 255), 1,
                        cv2.LINE_AA)

        # 添加说明
        im_rd = cv2.putText(im_rd, "s: save face", (20, 400), font, 0.8,
                            (255, 255, 255), 1, cv2.LINE_AA)
        im_rd = cv2.putText(im_rd, "q:quit", (20, 450), font, 0.8,
                            (255, 255, 255), 1, cv2.LINE_AA)

        # 按下q键退出
        if kk == ord('q'):
            break
        cv2.startWindowThread()
        cv2.imshow("camera", im_rd)

    # 窗口显示
    # cv2.namedWindow("camera", 0)   # 摄像头窗口大小可调

    # 释放摄像
    cap.release()
    cv2.destroyWindow('camera')
    # 删除窗口
    # 这里用数据库识别
    answer = recognition(path_save + "img_face_" + str(cnt_p) + ".jpg",
                         test_folder_path=test_folder_path,
                         threshold=0.6,
                         answer_pic=True)
    return answer
Exemple #50
0
def detectAndTrackMultipleFaces():
    global block
    #Open the first webcame device
    capture = cv2.VideoCapture(0)
    capture.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0)
    capture.set(cv2.CAP_PROP_EXPOSURE, 0.15)

    #Create two opencv named windows
    cv2.namedWindow("base-image", cv2.WINDOW_AUTOSIZE)
    cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE)

    #Position the windows next to eachother
    cv2.moveWindow("base-image", 0, 100)
    cv2.moveWindow("result-image", 400, 100)

    #Start the window thread for the two windows we are using
    cv2.startWindowThread()

    #The color of the rectangle we draw around the face
    rectangleColor = (0, 165, 255)

    #variables holding the current frame number and the current faceid
    frameCounter = 0
    currentFaceID = 0

    #Variables holding the correlation trackers and the name per faceid
    faceTrackers = {}
    faceNames = {}
    faceFeatures = {}

    cur_photo = None

    try:
        while True:
            #Retrieve the latest image from the webcam
            rc, fullSizeBaseImage = capture.read()
            cur_photo = fullSizeBaseImage.copy()

            #Resize the image to 320x240
            baseImage = cv2.resize(fullSizeBaseImage, (320, 240))

            #Check if a key was pressed and if it was Q, then break
            #from the infinite loop
            pressedKey = cv2.waitKey(2)
            if pressedKey == ord('Q'):
                break

            #Result image is the image we will show the user, which is a
            #combination of the original image from the webcam and the
            #overlayed rectangle for the largest face
            resultImage = baseImage.copy()

            #STEPS:
            # * Update all trackers and remove the ones that are not
            #   relevant anymore
            # * Every 10 frames:
            #       + Use face detection on the current frame and look
            #         for faces.
            #       + For each found face, check if centerpoint is within
            #         existing tracked box. If so, nothing to do
            #       + If centerpoint is NOT in existing tracked box, then
            #         we add a new tracker with a new face-id

            #Increase the framecounter
            frameCounter += 1

            #Update all the trackers and remove the ones for which the update
            #indicated the quality was not good enough
            fidsToDelete = []
            for fid in faceTrackers.keys():
                trackingQuality = faceTrackers[fid].update(baseImage)

                #If the tracking quality is good enough, we must delete
                #this tracker
                if trackingQuality < 7:
                    fidsToDelete.append(fid)

            for fid in fidsToDelete:
                print("Removing fid " + str(fid) + " from list of trackers")
                faceTrackers.pop(fid, None)

            #Every 10 frames, we will have to determine which faces
            #are present in the frame
            if (frameCounter % 10) == 0:

                #For the face detection, we need to make use of a gray
                #colored image so we will convert the baseImage to a
                #gray-based image
                gray = cv2.cvtColor(baseImage, cv2.COLOR_BGR2GRAY)

                #Now use the haar cascade detector to find all faces
                #in the image
                faces = faceCascade.detectMultiScale(gray, 1.3, 5)

                #Loop over all faces and check if the area for this
                #face is the largest so far
                #We need to convert it to int here because of the
                #requirement of the dlib tracker. If we omit the cast to
                #int here, you will get cast errors since the detector
                #returns numpy.int32 and the tracker requires an int
                for (_x, _y, _w, _h) in faces:
                    x = int(_x)
                    y = int(_y)
                    w = int(_w)
                    h = int(_h)

                    #calculate the centerpoint
                    x_bar = x + 0.5 * w
                    y_bar = y + 0.5 * h

                    #Variable holding information which faceid we
                    #matched with
                    matchedFid = None

                    #Now loop over all the trackers and check if the
                    #centerpoint of the face is within the box of a
                    #tracker
                    for fid in faceTrackers.keys():
                        tracked_position = faceTrackers[fid].get_position()

                        t_x = int(tracked_position.left())
                        t_y = int(tracked_position.top())
                        t_w = int(tracked_position.width())
                        t_h = int(tracked_position.height())

                        #calculate the centerpoint
                        t_x_bar = t_x + 0.5 * t_w
                        t_y_bar = t_y + 0.5 * t_h

                        #check if the centerpoint of the face is within the
                        #rectangleof a tracker region. Also, the centerpoint
                        #of the tracker region must be within the region
                        #detected as a face. If both of these conditions hold
                        #we have a match
                        if ((t_x <= x_bar <= (t_x + t_w)) and (t_y <= y_bar <=
                                                               (t_y + t_h))
                                and (x <= t_x_bar <=
                                     (x + w)) and (y <= t_y_bar <= (y + h))):
                            matchedFid = fid

                    #If no matched fid, then we have to create a new tracker
                    if matchedFid is None:
                        #TODO
                        print("Creating new tracker " + str(currentFaceID))
                        print('call')
                        cv2.imwrite('./imgs/frame.jpg', cur_photo)
                        # if cv2.waitKey(1) & 0xFF == ord('q'):
                        #     break
                        features = fe.parse_frame(cur_photo)
                        print(features)

                        #Create and store the tracker
                        tracker = dlib.correlation_tracker()
                        tracker.start_track(
                            baseImage,
                            dlib.rectangle(x - 10, y - 20, x + w + 10,
                                           y + h + 20))

                        faceTrackers[currentFaceID] = tracker
                        faceFeatures[currentFaceID] = features

                        #Start a new thread that is used to simulate
                        #face recognition. This is not yet implemented in this
                        #version :)
                        t = threading.Thread(target=doRecognizePerson,
                                             args=(faceNames, currentFaceID))
                        t.start()

                        #Increase the currentFaceID counter
                        currentFaceID += 1

            #Now loop over all the trackers we have and draw the rectangle
            #around the detected faces. If we 'know' the name for this person
            #(i.e. the recognition thread is finished), we print the name
            #of the person, otherwise the message indicating we are detecting
            #the name of the person
            for fid in faceTrackers.keys():
                tracked_position = faceTrackers[fid].get_position()

                t_x = int(tracked_position.left())
                t_y = int(tracked_position.top())
                t_w = int(tracked_position.width())
                t_h = int(tracked_position.height())

                cv2.rectangle(resultImage, (t_x, t_y), (t_x + t_w, t_y + t_h),
                              rectangleColor, 2)

                if fid in faceNames.keys():
                    if faceFeatures[fid] == None and not block:
                        print('call')
                        block = True
                        rc, fullSizeBaseImage = capture.read()
                        features = FeatureExtraction().parse_frame(
                            fullSizeBaseImage)
                        faceFeatures[fid] = features

                    if faceFeatures[fid] != None:
                        base = faceFeatures[fid][0]['attributes']
                        # print(base)
                        counter = 0
                        for key in base.keys():
                            string = ""
                            string += key + ' : '
                            string += str(base[key]['value'])
                            string += ','
                            cv2.putText(
                                resultImage, string,
                                (int(t_x + t_w / 2), int(t_y - counter * 10)),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255),
                                1)
                            counter += 1

                        # cv2.putText(resultImage, faceNames[fid] ,
                        #             (int(t_x + t_w/2), int(t_y)),
                        #             cv2.FONT_HERSHEY_SIMPLEX,
                        #             0.5, (255, 255, 255), 2)

                else:

                    cv2.putText(resultImage, "Detecting...",
                                (int(t_x + t_w / 2), int(t_y)),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255),
                                2)

            #Since we want to show something larger on the screen than the
            #original 320x240, we resize the image again
            #
            #Note that it would also be possible to keep the large version
            #of the baseimage and make the result image a copy of this large
            #base image and use the scaling factor to draw the rectangle
            #at the right coordinates.
            largeResult = cv2.resize(resultImage,
                                     (OUTPUT_SIZE_WIDTH, OUTPUT_SIZE_HEIGHT))

            #Finally, we want to show the images on the screen
            # cv2.imshow("base-image", baseImage)
            cv2.imshow("base-image", resultImage)
            cv2.imshow("result-image", largeResult)

    #To ensure we can also deal with the user pressing Ctrl-C in the console
    #we have to check for the KeyboardInterrupt exception and break out of
    #the main loop
    except KeyboardInterrupt as e:
        pass

    #Destroy any OpenCV windows and exit the application
    cv2.destroyAllWindows()
    exit(0)
Exemple #51
0
class VideoTracker(object):
    def __init__(self, cfg):
        use_cuda = torch.cuda.is_available()

        self.detector = build_detector(cfg, use_cuda=False)
        self.deepsort = build_tracker(cfg, use_cuda=False)
        self.class_names = self.detector.class_names

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, exc_traceback):
        if exc_type:
            print(exc_type, exc_value, exc_traceback)

    def doRecognizePerson(
        faceNames, fid
    ):  #------------------------------------------------------------------------------
        time.sleep(2)
        faceNames[fid] = "Person " + str(
            fid
        )  #-----------------------------------------------------------------------------

    #Start the window thread for the two windows we are using
    cv2.startWindowThread()

    #The color of the rectangle we draw around the face
    rectangleColor = (0, 165, 255)

    #variables holding the current frame number and the current faceid
    frameCounter = 0
    currentFaceID = 0

    #Variables holding the correlation trackers and the name per faceid
    faceTrackers = {}
    faceNames = {}

    def track(self):
        count = 0

        global temp

        cap = cv2.VideoCapture(0)

        #Start the window thread for the two windows we are using
        cv2.startWindowThread()

        #The color of the rectangle we draw around the face
        rectangleColor = (0, 165, 255)

        #variables holding the current frame number and the current faceid
        frameCounter = 0
        currentFaceID = 0

        #Variables holding the correlation trackers and the name per faceid
        faceTrackers = {}
        faceNames = {}

        while True:
            global c3
            global c4
            ret, frame = cap.read()
            frame = cv2.flip(frame, 1)
            Width = frame.shape[1]
            Height = frame.shape[0]
            blob = cv2.dnn.blobFromImage(frame,
                                         1.0 / 255.0, (416, 416), [0, 0, 0],
                                         True,
                                         crop=False)
            net.setInput(blob)
            outs = net.forward(getOutputsNames(net))

            c1 = 0  #real-time female count
            c2 = 0  #real-time male count
            class_ids = []
            confidences = []
            boxes = []
            conf_threshold = 0.5
            nms_threshold = 0.4

            for out in outs:
                #print(out.shape)
                for detection in out:

                    #each detection  has the form like this [center_x center_y width height obj_score class_1_score class_2_score ..]
                    scores = detection[5:]  #classes scores starts from index 5
                    class_id = np.argmax(scores)
                    confidence = scores[class_id]
                    if confidence > 0.5:
                        #print("confidence",confidence)
                        center_x = int(detection[0] * Width)
                        center_y = int(detection[1] * Height)
                        w = int(detection[2] * Width)
                        h = int(detection[3] * Height)
                        x = center_x - w / 2
                        y = center_y - h / 2
                        class_ids.append(class_id)
                        print("class_ids", class_ids)
                        confidences.append(float(confidence))
                        #print("confidences",confidences)
                        boxes.append([x, y, w, h])

    # apply  non-maximum suppression algorithm on the bounding boxes
            indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold,
                                       nms_threshold)

            for i in indices:
                i = i[0]
                box = boxes[i]
                x = box[0]
                y = box[1]
                w = box[2]
                h = box[3]
                if class_ids[i] == 0 or class_ids[i] == 3 or class_ids[
                        i] == 5 or class_ids[i] == 7:
                    c1 = c1 + 1
                elif class_ids[i] == 1 or class_ids[i] == 2 or class_ids[
                        i] == 4 or class_ids[i] == 6:
                    c2 = c2 + 1
                else:
                    pass
                print("Num of live female viewers:", c1)
                print("Num of live male viewers:", c2)
                draw_pred(frame, class_ids[i], confidences[i], round(x),
                          round(y), round(x + w), round(y + h))

            face_locations = face_recognition.face_locations(frame)

            #for top, right, bottom, left in face_locations:
            #    cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

            #----------------------------------------------------------------

            frameCounter += 1  #------------------------------------------------------------------------------
            start = time.time()

            bbox_xywh, cls_conf, cls_ids = self.detector(frame)

            if bbox_xywh is not None:
                #print("No of live viewers: ",len(face_locations))
                count = 0

                mask = cls_ids == 0
                bbox_xywh[:,
                          3:] *= 1.2  # bbox dilation just in case bbox too small

                cls_conf = cls_conf[mask]

                outputs = self.deepsort.update(bbox_xywh, cls_conf,
                                               frame)  #left,top,right,bottom
                face_locations = face_recognition.face_locations(frame)
                #print("fl",face_locations)
                #for top, right, bottom, left in face_locations:
                #   cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) #for face detector box

                print("No of live viewers: ", len(face_locations))
                temp1 = []
                for i in range(len(face_locations)):
                    t1, t2, t3, t4 = face_locations[i]
                    temp = centre(t1, t2, t3, t4)
                    temp1.append(temp)
                #print("Outputs: ",outputs)
                #print(len(outputs))
                for i in range(len(outputs)):
                    if outputs[i][4] not in total_p:
                        total_p.append(outputs[i][4])
                        #print(temp)
                print("Total No of people: ", len(outputs))
                # draw boxes for visualization
                if len(outputs) > 0:
                    bbox_xyxy = outputs[:, :-1]
                    identities = outputs[:, -1]
                    frame = draw_boxes(frame, bbox_xyxy,
                                       identities)  #for tracker box

                for i in range(len(outputs)):
                    if (outputs[i][4] not in nv):
                        for j in range(len(temp1)):
                            a, b, c, d, e = outputs[i]
                            flag = FindPoint(a, b, c, d, temp1[j][0],
                                             temp1[j][1])
                            #print(flag)
                            if flag:
                                if e not in nv:
                                    nv.append(e)
                                    time.sleep(4)
                                    if class_ids[i] == 0 or class_ids[
                                            i] == 3 or class_ids[
                                                i] == 5 or class_ids[i] == 7:
                                        c4 = c4 + 1
                                    elif class_ids[i] == 1 or class_ids[
                                            i] == 2 or class_ids[
                                                i] == 4 or class_ids[i] == 6:
                                        c3 = c3 + 1
                                    else:
                                        pass

            end = time.time()
            print("time: {:.03f}s, fps: {:.03f}".format(
                end - start, 1 / (end - start)))

            cv2.imshow("frame", frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        #print(count)
        cap.release()
        cv2.destroyAllWindows()
        return [nv, total_p, c3, c4]
Exemple #52
0
 def connect(self):
     self.serial.open()
     while not self.isImageStart():
         pass
     print("finish init camera")
     cv2.startWindowThread()
Exemple #53
0
class VideoTracker(object):
    def __init__(self, cfg):
        use_cuda = torch.cuda.is_available()
        if not use_cuda:
            raise UserWarning("Running in cpu mode!")

        self.detector = build_detector(cfg, use_cuda=True)
        self.deepsort = build_tracker(cfg, use_cuda=True)
        self.class_names = self.detector.class_names

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, exc_traceback):
        if exc_type:
            print(exc_type, exc_value, exc_traceback)

    def doRecognizePerson(faceNames, fid):
        time.sleep(2)
        faceNames[fid] = "Person " + str(fid)

    # Start the window thread for the two windows we are using
    cv2.startWindowThread()

    # The color of the rectangle we draw around the face
    rectangleColor = (0, 165, 255)

    # variables holding the current frame number and the current faceid
    frameCounter, currentFaceID = 0, 0

    # Variables holding the correlation trackers and the name per faceid
    faceTrackers, faceNames = {}, {}

    def track(self):
        print("inside track")
        outputs_length = 0
        count = 0
        global temp
        cap = cv2.VideoCapture(0)

        # Start the window thread for the two windows we are using
        cv2.startWindowThread()

        # The color of the rectangle we draw around the face
        rectangleColor = (0, 165, 255)

        # variables holding the current frame number and the current faceid
        frameCounter, currentFaceID = 0, 0
        # Variables holding the correlation trackers and the name per faceid
        faceTrackers, faceNames, face_locations_len_previous, outputs_len_previous = {}, {}, 0, 0
        while True:
            global c3
            global c4
            full = timer()
            ret, frame = cap.read()
            gray_img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
            face_locations = faceCascade.detectMultiScale(gray_img,
                                                          scaleFactor=1.3,
                                                          minNeighbors=4,
                                                          minSize=(40, 40))
            frame = cv2.flip(frame, 1)
            frame = cv2.resize(frame, (1080, 1080))
            gray_img = cv2.flip(frame, 1)
            gray_img = cv2.resize(frame, (1080, 1080))
            face_locations_haar = faceCascade.detectMultiScale(gray_img,
                                                               scaleFactor=1.3,
                                                               minNeighbors=4,
                                                               minSize=(40,
                                                                        40))
            face_locations = change_coor(face_locations_haar)
            frameCounter += 1
            half = timer()
            bbox_xywh, cls_conf, cls_ids = self.detector(frame)
            resultImg, faceBoxes = highlightFace(faceNet, frame)
            #if(outputs_length==0):
            #    ws.send(json.dumps({'users_count': 0})) #'current_users':face_locations_length,'male_users':c1,'female_users':c2}))#'total_users_count':total_p,'total_current_users':nv,'total_male_users':c3,'total_female_users':c4}))

            #outputs = self.deepsort.update(bbox_xywh, cls_conf, frame)
            ##            if (bbox_xywh is None)==0:
            ##                ws.send(json.dumps({'users_count': 0}))
            if (bbox_xywh is not None) & (frameCounter % 1 == 0):
                count, start = 0, timer()
                mask = cls_ids == 0
                bbox_xywh[:, 3:] *= 1.2
                c1 = 0
                c2 = 0

                cls_conf = cls_conf[mask]
                start = timer()
                outputs = self.deepsort.update(bbox_xywh, cls_conf, frame)
                outputs_length = len(outputs)
                print("output length: ", outputs_length)
                start = timer()
                if outputs_length < 1:
                    ws.send(
                        json.dumps({
                            'users_count': 0,
                            'current_users': 0,
                            'male_users': c1,
                            'female_users': c2
                        })
                    )  #'total_users_count':total_p,'total_current_users':nv,'total_male_users':c3,'total_female_users':c4}))

                #face_locations = face_R(frame)
                #print("FUNCTION maa atli waar laagi ", timer()-start)

                #face_locations_length = len(face_locations)
                face_locations_length = len(face_locations)
                #if(face_locations_length)==0:
                #    ws.send(json.dumps({'users_count': outputs_length,'current_users':face_locations_length,'male_users':c1,'female_users':c2}))#'total_users_count':total_p,'total_current_users':nv,'total_male_users':c3,'total_female_users':c4}))

                start = timer()
                for top, right, bottom, left in face_locations:
                    cv2.rectangle(frame, (left, top), (right, bottom),
                                  (0, 255, 0), 2)

                for fc in faceBoxes:
                    face = frame[max(0, fc[1] -
                                     padding):min(fc[3] +
                                                  padding, frame.shape[0] - 1),
                                 max(0, fc[0] -
                                     padding):min(fc[2] +
                                                  padding, frame.shape[1] - 1)]
                    blob = cv2.dnn.blobFromImage(face,
                                                 1.0, (227, 227),
                                                 MODEL_MEAN_VALUES,
                                                 swapRB=False)
                    genderNet.setInput(blob)
                    genderPreds = genderNet.forward()
                    gender = genderList[genderPreds[0].argmax()]
                    if gender == 'Male':
                        #print("Male detected")
                        c1 = c1 + 1
                    elif gender == 'Female':
                        #print("Female detected")
                        c2 = c2 + 1
                        ##                            for top, right, bottom, left in face_locations:
                        ##                                [x,y] = centre(top,right,bottom,left)
                        ##                               # temp2.append([x,y])  #list of centres for gender
                        ##                                if [x,y] == [0,0] :
                        ##                                   [x,y] = 'NA'
                        ##                                else :
                        ##                                    info = [[x,y],gender]
                        ##                                    tk.append(info)
                        #print("info",tk)

                        print('Number of Male detected', c1)
                        print('Number of Female detected', c2)

                temp1 = []
                start = timer()
                if outputs_length >= outputs_len_previous:
                    for i in range(len(face_locations)):
                        t1, t2, t3, t4 = face_locations[i]
                        temp = centre(t1, t2, t3, t4)
                        temp1.append(temp)
                    #print("Centre Coordinmates",temp1)

                for i in range(len(outputs)):
                    if outputs[i][4] not in total_p:
                        total_p.append(outputs[i][4])

                start = timer()
                if len(outputs) > 0:
                    bbox_xyxy = outputs[:, :-1]
                    identities = outputs[:, -1]
                    frame = draw_boxes(frame, bbox_xyxy, identities)
                start = timer()
                if outputs_length >= outputs_len_previous:
                    #print("yes,andar aavyo")
                    for i in range(len(outputs)):
                        if (outputs[i][4] not in nv):
                            #print("che toh nai")
                            for j in range(len(temp1)):
                                a, b, c, d, e = outputs[i]
                                flag = FindPoint(a, b, c, d, temp1[j][0],
                                                 temp1[j][1])
                                if flag == True:
                                    if e not in nv:
                                        # print("navu print karyu")
                                        nv.append(e)
                                        gen = func(temp1[j][0], temp1[j][1],
                                                   outputs_length,
                                                   outputs_len_previous,
                                                   faceBoxes, frame)
                                        ##                                        #print("Answer",gender)
                                        time.sleep(1)
                                        if gen == 'Male':
                                            c3 = c3 + 1
                                        elif gen == 'Female':
                                            c4 = c4 + 1
                                        else:
                                            gen = None
                                else:
                                    #print("print nai thaay")
                                    pass
                #asyncio.get_event_loop().run_until_complete(command_receiver(outputs_length,face_locations_length,c1,c2,0,0,0,0))
                ws.send(
                    json.dumps({
                        'users_count': outputs_length,
                        'current_users': face_locations_length,
                        'male_users': c1,
                        'female_users': c2
                    })
                )  #'total_users_count':total_p,'total_current_users':nv,'total_male_users':c3,'total_female_users':c4}))
                outputs_len_previous = outputs_length
            else:
                ws.send(json.dumps({'users_count': 0}))
            cv2.imshow("frame", frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                #asyncio.get_event_loop().run_until_complete(command_receiver(outputs_length,face_locations_length,c1,c2,len(total_p),len(nv),c3,c4))
                ws.send(
                    json.dumps({
                        'users_count': outputs_length,
                        'current_users': face_locations_length,
                        'male_users': c1,
                        'female_users': c2,
                        'total_users_count': len(total_p),
                        'total_current_users': len(nv),
                        'total_male_users': c3,
                        'total_female_users': c4
                    }))
                #ws.send(json.dumps({'users_count': outputs_length,'current_users':face_locations_length,'male_users':c1,'female_users':c2}))
                ws.close()
                break

        cap.release()
        cv2.destroyAllWindows()
        return [nv, total_p, c3, c4]

    def run_ml(self):
        return self.track()
Exemple #54
0
def init_opencv():
    cv2.startWindowThread()
Exemple #55
0
import sys
import FindResult
reload(sys)
sys.setdefaultencoding('utf8')

cap = cv2.VideoCapture(0)

cap.set(3, 1920)
cap.set(4, 1080)
cap.set(1, 10.0)

while cap.isOpened():
    isSuccess, frame = cap.read()
    if isSuccess:
        cv2.namedWindow('myCapture', 0)
        cv2.startWindowThread()  #加在这个位置
        cv2.imshow("myCapture", frame)
        cv2.imwrite("opencvPic.jpeg", frame)
        time.sleep(0.1)
        ocr = OCR()
        text = ocr.startWithFile('opencvPic.jpeg')
        print text
        if text:
            if len(text) > 3:
                FindResult.FindShowBaidu(text)
                time.sleep(0.2)

    if cv2.waitKey(1) & 0xff == ord('q'):
        cv2.imwrite("opencvPic.jpeg", frame)

cap.release()
Exemple #56
0
    def track(self):
        count = 0

        global temp
        #import url
        import numpy as np
        import cv2

        #Create two opencv named windows
        #cv2.namedWindow("base-image", cv2.WINDOW_AUTOSIZE)             #--------------------------------------------------------------
        # cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE)           #--------------------------------------------------------------

        #Position the windows next to eachother
        #cv2.moveWindow("base-image",0,100)                                                     #--------------------------------------------------------------
        #cv2.moveWindow("result-image",400,100)                                             #---------------------------------------------------------------
        #cap = cv2.VideoCapture('http://192.168.137.61:80/')#.dtype('uint32')
        cap = cv2.VideoCapture(0)

        #Start the window thread for the two windows we are using
        cv2.startWindowThread()

        #The color of the rectangle we draw around the face
        rectangleColor = (0, 165, 255)

        #variables holding the current frame number and the current faceid
        frameCounter = 0
        currentFaceID = 0

        #Variables holding the correlation trackers and the name per faceid
        faceTrackers = {}
        faceNames = {}

        while True:
            ret, frame = cap.read()
            frame = cv2.flip(frame, 1)
            frame = cv2.resize(frame, (1080, 1080))
            #baseImage = cv2.resize( frame, ( 1080, 1080))   #-------------------------------------------------------
            #frame = baseImage[:, :, ::-1]
            face_locations = face_recognition.face_locations(frame)
            #print("Face location: ",face_locations)

            #for top, right, bottom, left in face_locations:
            #    cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

            #resultImage = baseImage.copy()          #----------------------------------------------------------------

            frameCounter += 1  #------------------------------------------------------------------------------

            bbox_xywh, cls_conf, cls_ids = self.detector(frame)
            #cv2.putText(frame,str(bbox_xywh),(0,15), cv2.FONT_HERSHEY_PLAIN,1,(255,255,255) ,2)
            if bbox_xywh is not None:
                #print("No of live viewers: ",len(face_locations))
                count = 0

                #print("Centre of face",temp)
                #print("Face loactions "+str(face_locations)+"Person: "+str(bbox_xywh))
                #flag=1
                mask = cls_ids == 0
                bbox_xywh[:,
                          3:] *= 1.2  # bbox dilation just in case bbox too small

                cls_conf = cls_conf[mask]

                outputs = self.deepsort.update(bbox_xywh, cls_conf,
                                               frame)  #left,top,right,bottom
                face_locations = face_recognition.face_locations(frame)
                for top, right, bottom, left in face_locations:
                    cv2.rectangle(frame, (left, top), (right, bottom),
                                  (0, 0, 255), 2)

                print("No of live viewers: ", len(face_locations))
                temp1 = []
                for i in range(len(face_locations)):
                    t1, t2, t3, t4 = face_locations[i]
                    temp = centre(t1, t2, t3, t4)
                    temp1.append(temp)
                #print("Outputs: ",outputs)
                #print(len(outputs))
                for i in range(len(outputs)):
                    if outputs[i][4] not in total_p:
                        total_p.append(outputs[i][4])
                        #print(temp)
                print("Total No of people: ", len(outputs))
                # draw boxes for visualization
                if len(outputs) > 0:
                    bbox_xyxy = outputs[:, :-1]
                    identities = outputs[:, -1]
                    frame = draw_boxes(frame, bbox_xyxy, identities)

                for i in range(len(outputs)):
                    if (outputs[i][4] not in nv):
                        for j in range(len(temp1)):
                            a, b, c, d, e = outputs[i]
                            flag = FindPoint(a, b, c, d, temp1[j][0],
                                             temp1[j][1])
                            #print(flag)
                            if flag:
                                if e not in nv:
                                    nv.append(e)
                            #print("nv",nv)

            cv2.imshow("frame", frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        #print(count)
        cap.release()
        cv2.destroyAllWindows()
        #print("No of People detected : ",len(temp))
        #print(temp)
        return [nv, total_p]
Exemple #57
0
    def undistort_movie(self, ofnam=None, frameint=25, crf=12, write=True, display=True, tmp_dir=None, crop=False):
        if self.movie is None:
            raise ArgusError('no movie specified')

        if (self.omni is None) and (self.coefficients is None):
            raise ArgusError('no distortion coefficients specified')

        if tmp_dir is None:
            tmp = tempfile.mkdtemp()
        else:
            tmp = tmp_dir

        print(tmp)

        print('Ripping audio... \n')
        sys.stdout.flush()
        cmd = [
            get_setting("FFMPEG_BINARY"),
            '-loglevel', 'panic',
            '-hide_banner',
            '-i', self.infilename,
            '-c:a', 'copy',
            '-vn', '-sn',
            tmp + '/' + 'temp.m4a']
        subprocess.call(cmd)

        if os.path.exists(tmp + '/' + 'temp.m4a'):
            statinfo = os.stat(tmp + '/' + 'temp.m4a')

            if statinfo.st_size != 0:
                audio = tmp + '/' + 'temp.m4a'
            else:
                audio = None
        else:
            audio = None

        if audio is not None:
            cmd = [get_setting("FFMPEG_BINARY"), '-y', '-f', 'rawvideo', \
                   '-vcodec', 'rawvideo', '-s', '{0}x{1}'.format(self.w, self.h), '-pix_fmt', 'rgb24', \
                   '-r', str(self.fps), '-i', '-', '-i', audio, \
                   '-acodec', 'copy', '-vcodec', 'libx264', '-preset', 'medium', '-crf', \
                   str(crf), '-g', str(frameint), '-pix_fmt', 'yuv420p', '-profile', 'baseline', '-threads', \
                   '0', '-pix_fmt', 'yuv420p', str(ofnam)]
        else:
            cmd = [get_setting("FFMPEG_BINARY"), '-y', '-f', 'rawvideo', \
                   '-vcodec', 'rawvideo', '-s', '{0}x{1}'.format(self.w, self.h), '-pix_fmt', 'rgb24', \
                   '-r', str(self.fps), '-i', '-', '-an', \
                   '-acodec', 'copy', '-vcodec', 'libx264', '-preset', 'medium', '-crf', \
                   str(crf), '-g', str(frameint), '-pix_fmt', 'yuv420p', '-profile', 'baseline', '-threads', \
                   '0', '-pix_fmt', 'yuv420p', str(ofnam)]

        if write:
            if ofnam == '':
                raise ArgusError('no output filename specified')
            _ = ofnam.split('.')
            if _[len(_) - 1] != 'mp4' and _[len(_) - 1] != 'MP4':
                raise ArgusError('output filename must specify an mp4 file')
                return
            if os.path.isfile(ofnam):
                raise ArgusError('output file already exists! Argus will not overwrite')
                return
        try:
            t = open(self.infilename, "r")
        except:
            raise ArgusError('input movie not found')
            return

        t = None

        if not self.coefficients is None:
            map1, map2 = self.get_mappings(crop)

        # if we're displaying, make a window to do so

        print('Beginning to undistort images and compile with FFMPEG...')
        sys.stdout.flush()

        p = Popen(cmd, stdin=PIPE)

        if display:
            if 'linux' in sys.platform:
                cv2.imshow("Undistorted", np.zeros((1080, 1920, 3)))
            cv2.namedWindow("Undistorted")

        if write:
            # Create a list of the frames (pngs)
            fileList = []
        k = 1

        if display:
            if not 'linux' in sys.platform:
                cv2.startWindowThread()

        for a in range(int(self.movie.get(cv2.CAP_PROP_FRAME_COUNT))):
            retval, raw = self.movie.read()

            if retval:
                previous = raw
                if crop:
                    if self.oCamUndistorter is None:
                        undistorted = cv2.remap(raw, map1, map2, cv2.INTER_LINEAR)
                    else:
                        undistorted = self.oCamUndistorter.undistort_frame(raw)
                else:
                    if self.oCamUndistorter is None:
                        undistorted = cv2.remap(raw, map1, map2,
                                                interpolation=cv2.INTER_CUBIC,
                                                borderMode=cv2.BORDER_CONSTANT,
                                                borderValue=(211, 160, 86))
                    else:
                        undistorted = self.oCamUndistorter.undistort_frame(raw)

                if display:
                    cv2.imshow('Undistorted', cv2.resize(undistorted, (0, 0), fx=0.5, fy=0.5))
                    cv2.waitKey(1)
                undistorted = cv2.cvtColor(undistorted, cv2.COLOR_BGR2RGB)

                # im = Image.fromarray(undistorted, 'RGB')
                # im.save(p.stdin, 'PNG')

                if write:
                    p.stdin.write(undistorted.tostring())

                # line of code above allows you to open video clip after it is saved
                # without that line video clip will save but won't be able to open and play clip

                variable = False
                self.root = Tk()

                def __init__(self, master, title):
                    top = self.top = Toplevel(master)
                    top.resizable(width=FALSE, height=FALSE)
                    top.bind('<Return>', self.cleanup)
                    self.l = Label(top, text=title)
                    self.l.pack(padx=10, pady=10)
                    self.e = Entry(top)
                    self.e.focus_set()
                    self.e.pack(padx=10, pady=5)
                    self.b = Button(top, text='Ok', padx=10, pady=10)
                    self.b.bind('<Button-1>', self.cleanup)
                    self.b.pack(padx=5, pady=5)
                    self.crop = StringVar(self.root)
                    self.copy = StringVar(self.root)

                self.wrdispboth = IntVar(self.root)

                if a % 5 == 0:
                    print('\n', end='')
                    sys.stdout.flush()
                """
                # Write the individual frame as a png to the temporary directory
                if write:
                    cv2.imwrite(tmp + '/' + str(a) + '.png', undistorted)
                    fileList.append(tmp + '/' + str(a) + '.png')
                """
            else:
                if write:
                    print("Could not read frame number: " + str(a) + "\n writing blank frame")
                    p.stdin.write(np.zeros_like(previous).tostring())
                    sys.stdout.flush()
                else:
                    print("Could not read frame number: " + str(a))
                    sys.stdout.flush()

        p.stdin.close()
        p.wait()

        print('Undistortion finished')
        if write:
            print('Wrote mp4 to {0}'.format(ofnam))
        sys.stdout.flush()
        # Destroy movie object and the window if it was even created
        movie = None
        if display:
            cv2.waitKey(1)
            cv2.destroyAllWindows()
            cv2.waitKey(1)
        if write:
            # clip = ImageSequenceClip(fileList, fps=self.fps, with_mask = False, load_images = False)

            # Write the mp4 file
            """
            if os.path.exists(tmp + '/' + 'temp.m4a'):
                clip.write_videofile(ofnam, fps=self.fps, audio = tmp + '/' + 'temp.m4a', audio_fps = 48000, codec='libx264', threads=0, ffmpeg_params = ['-crf', str(crf), '-g', str(frameint), '-pix_fmt', 'yuv420p', '-profile' ,'baseline'])
            else:
                print('No audio stream found, writing without...')
                clip.write_videofile(ofnam, fps=self.fps, audio_fps = 48000, codec='libx264', threads=0, ffmpeg_params = ['-crf', str(crf), '-g', str(frameint), '-pix_fmt', 'yuv420p', '-profile' ,'baseline'])
            """
            sys.stdout.flush()
            clip = None

            # Destroy the temporary directory
            shutil.rmtree(tmp)
            if self.copy_tmp is not None:
                shutil.rmtree(self.copy_tmp)
Exemple #58
0
def detectAndTrackLargestFace():
    #Open the ip ptz camera
    #capture = cv2.VideoCapture('rtsp://admin:@192.168.1.110:554/1/h264major latency=0')
    capture = cv2.VideoCapture('rtsp://192.168.1.110:554/1/h264major')
	#if need to use web-cam use the 0 option
    #capture = cv2.VideoCapture(0)

    #Create two opencv named windows
    cv2.namedWindow("base-image", cv2.WINDOW_AUTOSIZE)
    cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE)

    #Position the windows next to eachother
    cv2.moveWindow("base-image",0,100)
    cv2.moveWindow("result-image",400,100)

    #Start the window thread for the two windows we are using
    cv2.startWindowThread()

    #Create the tracker we will use
    tracker = dlib.correlation_tracker()

    #The variable we use to keep track of the fact whether we are
    #currently using the dlib tracker
    trackingFace = 0

    #The color of the rectangle we draw around the tracking object face or upper body
    rectangleColor = (78,0,137)

    try:
        current_x = 0
        count = 2
        focused = True
        center_coords = 180
        max_error = 0
        h_error = 0
        custom_tracker = False
        no_rot_in_progress = False
        custom_sync = True
        start_timer = None
        custom_sync_main = True
        custom_sync_vertical = True
        start_timer_v = None
        time_required = 0
        start_tl = None
        check_vertical = False
        time_required_v = 0
        while True:
            #Retrieve the latest image from the ip-cam / webcam
            rc,fullSizeBaseImage = capture.read()

            #Resize the image to 320x240
            baseImage = cv2.resize( fullSizeBaseImage, ( 320, 240))

            #Check if a key was pressed and if it was Q, then destroy all
            #opencv windows and exit the application
            pressedKey = cv2.waitKey(2)
            if pressedKey == ord('Q'):
                cv2.destroyAllWindows()
                exit(0)

            #Result image is the image we will show the user, which is a
            #combination of the original image from the ip-cam / webcam and the
            #overlayed rectangle for the largest face
            resultImage = baseImage.copy()

            #If we are not tracking a face, then try to detect one
            if not trackingFace:

                #For the face detection, we need to make use of a gray
                #colored image so we will convert the baseImage to a
                #gray-based image
                gray = cv2.cvtColor(baseImage, cv2.COLOR_BGR2GRAY)
                #Now use the haar cascade detector to find all faces
                #in the image
                faces = faceCascade.detectMultiScale(gray, 1.3, 5)

                #print("Using the cascade detector to detect face")


                #we are only interested in the 'largest'
                #face in order to detect lecturer, and we determine 
				#this based on the largest
                #area of the found rectangle. First initialize the
                #required variables to 0
                maxArea = 0
                x = 0
                y = 0
                w = 0
                h = 0

                #Loop over all faces and check if the area for this
                #face is the largest so far
                #We need to convert it to int here because of the
                #requirement of the dlib tracker. If we omit the cast to
                #int here, you will get cast errors since the detector
                #returns numpy.int32 and the tracker requires an int
                for (_x,_y,_w,_h) in faces:
                    if _w*_h > maxArea:
                        x = int(_x)
                        y = int(_y)
                        w = int(_w)
                        h = int(_h)
                        maxArea = w*h

                #If one or more faces are found, initialize the tracker
                #on the largest face in the picture
                if maxArea > 0 :

                    #Initialize the tracker
                    tracker.start_track(baseImage,
                                        dlib.rectangle( x-10,
                                                        y-20,
                                                        x+w+10,
                                                        y+h+20))

                    #Set the indicator variable such that we know the
                    #tracker is tracking a region in the image
                    trackingFace = 1

            #Check if the tracker is actively tracking a region in the image
            if trackingFace:

                #Update the tracker and request information about the
                #quality of the tracking update
                trackingQuality = tracker.update( baseImage )

                #If the tracking quality is good enough, determine the
                #updated position of the tracked region and draw the
                #rectangle
                if trackingQuality >= 8.75:
                    #print("Detected Face")
                    start_tl = None
                    tracked_position =  tracker.get_position()

                    t_x = int(tracked_position.left())
                    t_y = int(tracked_position.top())
                    t_w = int(tracked_position.width())
                    t_h = int(tracked_position.height())
                    cv2.rectangle(resultImage, (t_x, t_y),
                                                (t_x + t_w , t_y + t_h),
                                                rectangleColor ,2)
                    # if count>1:
                    #    current_x = t_x
                    #    count-=1

                    #print("Coordinates : " + str(t_x) + ", "+ str(t_y) + ", "+ str(t_h) + ", "+ str(t_w))
                    #print("rec : " + str(t_x) + ", "+ str(t_y) + ", "+ str(t_x + t_w) + ", "+ str(t_y + t_h))
                    #print("Center Coordinates : " + str(t_w/2) + ", "+ str(t_h/2))

                    #max_error = 5

                    # vm_cd = int(t_x)
                    # if current_x<180 and focused:
                    #    rot_cen = (180-current_x)*22
                    #    print('rotation time required ' + str(rot_cen))
                    #    turn_left_ptz = execute_js('onvif_movement/turn_left.js',str(rot_cen))
                    #    if turn_left_ptz:
                    #        print('Focused Success - Turned Left')
                    #        focused = False
                    #        custom_tracker = True
                    #        no_rot_in_progress = True
                    #    else:
                    #        print('failed left')
                    # if current_x>180 and focused:
                    #    rot_cen = (current_x-180)*22
                    #    print('rotation time required ' + str(rot_cen))
                    #    turn_right_ptz = execute_js('onvif_movement/turn_right.js ',str(rot_cen))
                    #    if turn_right_ptz:
                    #        print('Focused Success - Turned Right')
                    #        focused = False
                    #        custom_tracker = True
                    #        no_rot_in_progress = True
                    #    else:
                    #        print('failed right')

                    custom_tracker = True #True
                    no_rot_in_progress = True
                    current_pos = t_x
                    h_error = abs(180 - current_pos)
                    #print('h_error ' + str(h_error))

                    if custom_tracker and h_error > 40:
                        print('Distance found! ' + str(h_error))
                        check_vertical = False
                        if custom_sync_main:
                            print('Tracker Standby')
                            if current_pos < 140:
                                no_rot_in_progress = False
                                #rot_cen = (180 - current_pos) * 22
                                rot_cen = abs((130 - t_x) * 11)
                                print('Rotation Time Required ' + str(rot_cen))

                                if start_timer is None:
                                    start_timer = timer()

                                turn_left_ptz = muterun_js('onvif_movement/turn_left.js', str(rot_cen))

                                custom_sync = True
                                rot_cen_in_sec = int(rot_cen / 1000)
                                time_required = rot_cen_in_sec + 2

                                end_timer = timer()
                                timer_diff = int(end_timer - start_timer)

                                if timer_diff >= time_required:
                                    custom_sync = True
                                else:
                                    custom_sync = False

                                if custom_sync:
                                    start_timer = None
                                    custom_sync_main = True
                                else:
                                    custom_sync_main = False

                                if turn_left_ptz.exitcode == 0:
                                    print('Custom Tracker - Turned Left')
                                    no_rot_in_progress = True
                                    #print(response.stdout)
                                else:
                                    print('failed left')
                                    #sys.stderr.write(response.stderr)

                            if current_pos > 220:
                                no_rot_in_progress = False
                                #rot_cen = (180 - current_pos) * 22
                                rot_cen = abs((130 - t_x) * 11)
                                print('Rotation Time Required ' + str(rot_cen))

                                if start_timer is None:
                                    start_timer = timer()

                                turn_left_ptz = muterun_js('onvif_movement/turn_right.js', str(rot_cen))

                                custom_sync = True
                                rot_cen_in_sec = int(rot_cen / 1000)
                                time_required = rot_cen_in_sec + 2

                                end_timer = timer()
                                timer_diff = int(end_timer - start_timer)

                                if timer_diff >= time_required:
                                    custom_sync = True
                                else:
                                    custom_sync = False

                                if custom_sync:
                                    start_timer = None
                                    custom_sync_main = True
                                else:
                                    custom_sync_main = False

                                if turn_left_ptz.exitcode == 0:
                                    print('Custom Tracker - Turned Right')
                                    no_rot_in_progress = True
                                    # print(response.stdout)
                                else:
                                    print('failed right')

                        else:
                            print('Tracker Waiting!')
                            #custom_tracker = False

                            end_timer = timer()
                            timer_diff = int(end_timer - start_timer)

                            if timer_diff >= time_required:
                                custom_sync = True
                            else:
                                custom_sync = False

                            if custom_sync:
                                start_timer = None
                                custom_sync_main = True
                            else:
                                custom_sync_main = False
                    else:
                        print('Lecturer Horizontal Distance Under Control! ' + str(h_error))
                        check_vertical = True

                    # vertical tracker controller
                    current_pos_vertical = t_y
                    v_error = abs(63 - current_pos_vertical)
                    if check_vertical and custom_sync_vertical and v_error > 22:
                        print('Vertical Distance Found! ' + str(v_error))
                        rot_cen_vertical = abs((63 - current_pos_vertical) * 12)
                        print('Vertical Rotation Time Required ' + str(rot_cen_vertical))
                        if current_pos_vertical < 40:
                            #print('Need to move down - script up')
                            if start_timer_v is None:
                                start_timer_v = timer()

                            turn_down_ptz = muterun_js('onvif_movement/turn_up.js', str(rot_cen_vertical))

                            rot_cen_v_in_sec = int(rot_cen_vertical / 1000)
                            time_required_v = rot_cen_v_in_sec + 2

                            end_timer_v = timer()
                            timer_diff_v = int(end_timer_v - start_timer_v)

                            if timer_diff_v >= time_required_v:
                                start_timer_v = None
                                custom_sync_vertical = True
                            else:
                                custom_sync_vertical = False

                            if turn_down_ptz.exitcode == 0:
                                print('Vertical Tracker - Turned Down')
                            else:
                                print('failed down')

                        if current_pos_vertical > 85:
                            #print('Need to move up script down')
                            if start_timer_v is None:
                                start_timer_v = timer()

                            turn_down_ptz = muterun_js('onvif_movement/turn_down.js', str(rot_cen_vertical))

                            rot_cen_v_in_sec = int(rot_cen_vertical / 1000)
                            time_required_v = rot_cen_v_in_sec + 2

                            end_timer_v = timer()
                            timer_diff_v = int(end_timer_v - start_timer_v)

                            if timer_diff_v >= time_required_v:
                                start_timer_v = None
                                custom_sync_vertical = True
                            else:
                                custom_sync_vertical = False

                            if turn_down_ptz.exitcode == 0:
                                print('Vertical Tracker - Turned Up')
                            else:
                                print('failed up')
                    else:
                        print('Lecturer Vertical Distance Under Control! ' + str(v_error))
                        #print('check vert ' + str(check_vertical))
                        #print('check custom_sync_main ' + str(custom_sync_main))
                        #print('check custom_sync_vertical ' + str(custom_sync_vertical))
                        #if check_vertical and custom_sync_main and not custom_sync_vertical:
                        if check_vertical and not custom_sync_vertical:
                            print('unlocked vertical locked within')
                            # custom_sync_vertical = True
                            end_timer_v = timer()
                            timer_diff_v = int(end_timer_v - start_timer_v)

                            if timer_diff_v >= time_required_v:
                                start_timer_v = None
                                custom_sync_vertical = True
                            else:
                                custom_sync_vertical = False

                        #print (custom_sync_vertical)

                    # print('v_error ' + str(current_pos_vertical))

                    # end of vertical tracker

                else:
                    #If the quality of the tracking update is not
                    #sufficient (e.g. the tracked region moved out of the
                    #screen) we stop the tracking of the face and in the
                    #next loop we will find the largest face in the image
                    #again
                    trackingFace = 0
                    print("Tracking Lost")

            else:
                if start_tl is None:
                    start_tl = time.time()
                end_tl = time.time()
                diff_tl = abs(end_tl - start_tl)
                # wait for 10 seconds
                if diff_tl >= 10:
                    print('Camera will be Recalibrated!')
                    recalibrate_ptz = execute_js('onvif_movement/recal_lost_lec.js')
                    if recalibrate_ptz:
                        print('Recalibrate Success')
                        start_tl = None
                    else:
                        print('Recalibrate Failure')

                else:
                    print('Waiting for Lecturer!')

            #Since we want to show something larger on the screen than the
            #original 320x240, we resize the image again
            #
            #Note that it would also be possible to keep the large version
            #of the base-image and make the result image a copy of this large
            #base image and use the scaling factor to draw the rectangle
            #at the right coordinates.
            largeResult = cv2.resize(resultImage,
                                     (OUTPUT_SIZE_WIDTH,OUTPUT_SIZE_HEIGHT))

            #Finally, we want to show the images on the screen
            cv2.imshow("base-image", baseImage)
            cv2.imshow("result-image", largeResult)

    #To ensure we can also deal with the user pressing Ctrl-C in the console
    #we have to check for the KeyboardInterrupt exception and destroy
    #all opencv windows and exit the application
    except KeyboardInterrupt as e:
        #turn_recal_ptz = execute_js('onvif_movement/recalibrate.js')
        cv2.destroyAllWindows()
        exit(0)
Exemple #59
0
def retrieve_corners_from_image(frame: np.ndarray, ):
    """

    Opens a window and waits for the user to click on all corners of the polygonal arena.
    The user should click on the corners in sequential order.

    Args:
        frame (np.ndarray): Frame to display.

    Returns:

        corners (np.ndarray): nx2 array containing the x-y coordinates of all n corners.

    """

    corners = []

    def click_on_corners(event, x, y, flags, param):
        # Callback function to store the coordinates of the clicked points
        nonlocal corners, frame

        if event == cv2.EVENT_LBUTTONDOWN:
            corners.append((x, y))

    # Create a window and display the image
    cv2.startWindowThread()

    while True:
        frame_copy = frame.copy()

        cv2.imshow(
            "deepof - Select polygonal arena corners - (q: exit / d: delete)",
            frame_copy,
        )
        cv2.setMouseCallback(
            "deepof - Select polygonal arena corners - (q: exit / d: delete)",
            click_on_corners,
        )

        # Display already selected corners
        if len(corners) > 0:
            for c, corner in enumerate(corners):
                cv2.circle(frame_copy, (corner[0], corner[1]), 4,
                           (40, 86, 236), -1)
                # Display lines between the corners
                if len(corners) > 1 and c > 0:
                    cv2.line(
                        frame_copy,
                        (corners[c - 1][0], corners[c - 1][1]),
                        (corners[c][0], corners[c][1]),
                        (40, 86, 236),
                        2,
                    )

        # Close the polygon
        if len(corners) > 2:
            cv2.line(
                frame_copy,
                (corners[0][0], corners[0][1]),
                (corners[-1][0], corners[-1][1]),
                (40, 86, 236),
                2,
            )

        cv2.imshow(
            "deepof - Select polygonal arena corners - (q: exit / d: delete)",
            frame_copy,
        )

        # Remove last added coordinate if user presses 'd'
        if cv2.waitKey(1) & 0xFF == ord("d"):
            corners = corners[:-1]

        # Exit is user presses q
        elif cv2.waitKey(1) & 0xFF == ord("q"):
            cv2.destroyAllWindows()
            cv2.waitKey(1)
            break

    cv2.destroyAllWindows()
    cv2.waitKey(1)

    # Return the corners
    return corners
def detectAndTrackLargestFace():
    #Open the first webcame device
    capture = cv2.VideoCapture(0)

    #Create two opencv named windows
    cv2.namedWindow("base-image", cv2.WINDOW_AUTOSIZE)
    cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE)
    cv2.namedWindow("mosaic-image", cv2.WINDOW_AUTOSIZE)

    #Position the windows next to eachother
    cv2.moveWindow("base-image", 0, 0)
    cv2.moveWindow("result-image", 400, 0)
    cv2.moveWindow("mosaic-image", 0, 400)

    #Start the window thread for the two windows we are using
    cv2.startWindowThread()

    #Create the tracker we will use
    tracker = dlib.correlation_tracker()

    #The variable we use to keep track of the fact whether we are
    #currently using the dlib tracker
    trackingFace = 0

    #The color of the rectangle we draw around the face
    rectangleColor = (0, 165, 255)

    try:
        while True:
            #Retrieve the latest image from the webcam
            rc, fullSizeBaseImage = capture.read()

            #Resize the image to 320x240
            baseImage = cv2.resize(fullSizeBaseImage, (320, 240))

            #Check if a key was pressed and if it was Q, then destroy all
            #opencv windows and exit the application
            if cv2.waitKey(1) & 0xFF == ord('Q'):
                quit()

            #Result image is the image we will show the user, which is a
            #combination of the original image from the webcam and the
            #overlayed rectangle for the largest face
            resultImage = baseImage.copy()
            mosaicImage = baseImage.copy()

            #If we are not tracking a face, then try to detect one
            if not trackingFace:

                #For the face detection, we need to make use of a gray
                #colored image so we will convert the baseImage to a
                #gray-based image
                gray = cv2.cvtColor(baseImage, cv2.COLOR_BGR2GRAY)
                #Now use the haar cascade detector to find all faces
                #in the image
                faces = faceCascade.detectMultiScale(gray, 1.3, 5)

                #In the console we can show that only now we are
                #using the detector for a face
                print("Using the cascade detector to detect face")

                #For now, we are only interested in the 'largest'
                #face, and we determine this based on the largest
                #area of the found rectangle. First initialize the
                #required variables to 0
                maxArea = 0
                x = 0
                y = 0
                w = 0
                h = 0

                #Loop over all faces and check if the area for this
                #face is the largest so far
                #We need to convert it to int here because of the
                #requirement of the dlib tracker. If we omit the cast to
                #int here, you will get cast errors since the detector
                #returns numpy.int32 and the tracker requires an int
                for (_x, _y, _w, _h) in faces:
                    if _w * _h > maxArea:
                        x = int(_x)
                        y = int(_y)
                        w = int(_w)
                        h = int(_h)
                        maxArea = w * h

                #If one or more faces are found, initialize the tracker
                #on the largest face in the picture
                if maxArea > 0:

                    #Initialize the tracker
                    tracker.start_track(
                        baseImage,
                        dlib.rectangle(x - 10, y - 20, x + w + 10, y + h + 20))

                    #Set the indicator variable such that we know the
                    #tracker is tracking a region in the image
                    trackingFace = 1

            #Check if the tracker is actively tracking a region in the image
            if trackingFace:

                #Update the tracker and request information about the
                #quality of the tracking update
                trackingQuality = tracker.update(baseImage)

                #If the tracking quality is good enough, determine the
                #updated position of the tracked region and draw the
                #rectangle
                if trackingQuality >= 5.5:
                    tracked_position = tracker.get_position()

                    t_x = int(tracked_position.left())
                    t_y = int(tracked_position.top())
                    t_w = int(tracked_position.width())
                    t_h = int(tracked_position.height())

                    # print("tracked", t_x, t_y, t_w, t_h)

                    cv2.rectangle(resultImage, (t_x, t_y),
                                  (t_x + t_w, t_y + t_h), rectangleColor, 2)

                    m_x = int(t_x > 0 and t_x or 0)
                    m_y = int(t_y > 0 and t_y or 0)
                    m_w = int(t_x + t_w < 320 and t_w or 320 - t_x)
                    m_h = int(t_y + t_h < 240 and t_h or 240 - t_y)

                    print("MOSAIC", m_x, m_y, m_w, m_h)
                    # print(int(m_w) * int(m_h))

                    face_img = mosaicImage[m_y:m_y + m_h, m_x:m_x + m_w]
                    face_img = cv2.resize(
                        face_img, (m_w // MOSAIC_RATE, m_h // MOSAIC_RATE))
                    face_img = cv2.resize(face_img, (m_w, m_h),
                                          interpolation=cv2.INTER_AREA)
                    mosaicImage[m_y:m_y + m_h, m_x:m_x + m_w] = face_img

                else:
                    #If the quality of the tracking update is not
                    #sufficient (e.g. the tracked region moved out of the
                    #screen) we stop the tracking of the face and in the
                    #next loop we will find the largest face in the image
                    #again
                    trackingFace = 0

            #Since we want to show something larger on the screen than the
            #original 320x240, we resize the image again
            #
            #Note that it would also be possible to keep the large version
            #of the baseimage and make the result image a copy of this large
            #base image and use the scaling factor to draw the rectangle
            #at the right coordinates.
            largeResult = cv2.resize(resultImage,
                                     (OUTPUT_SIZE_WIDTH, OUTPUT_SIZE_HEIGHT))
            mosaicResult = cv2.resize(mosaicImage,
                                      (OUTPUT_SIZE_WIDTH, OUTPUT_SIZE_HEIGHT))

            #Finally, we want to show the images on the screen
            cv2.imshow("base-image", baseImage)
            cv2.imshow("result-image", largeResult)
            cv2.imshow("mosaic-image", mosaicResult)

    #To ensure we can also deal with the user pressing Ctrl-C in the console
    #we have to check for the KeyboardInterrupt exception and destroy
    #all opencv windows and exit the application
    except KeyboardInterrupt as e:
        cv2.destroyAllWindows()
        exit(0)