예제 #1
0
    def find_main_marker(self, visible_markers):
        marker = None
        current_score = 0
        #the more dist_coeff is, the less main_marker will be distord
        dist_coeff = 5

        for m in visible_markers:
            verts = m['verts']
            size = (distance(verts[0], verts[1]) + distance(verts[1], verts[2]) + distance(verts[2], verts[3]) + distance(verts[3], verts[0])) / 4.

            vertical_distord = min(distance(verts[0], verts[1])/float(distance(verts[3], verts[2])),distance(verts[3], verts[2])/float(distance(verts[0], verts[1])))
            vertical_distord = vertical_distord * vertical_distord

            horizontal_distord = min(distance(verts[0], verts[3])/float(distance(verts[1], verts[2])),distance(verts[1], verts[2])/float(distance(verts[0], verts[3])))
            horizontal_distord = horizontal_distord * horizontal_distord
            distord_res = float(vertical_distord + horizontal_distord)/2
            distord_res = np.power(distord_res, dist_coeff)

            score = size * distord_res
            if score > current_score:
                current_score = score
                marker = m

        if marker != None:
            objp = gen_square_pattern_grid(marker['height'])
            # Find the rotation and translation vectors.

            if not self.show_undistord:
                verts = cv2.undistortPoints( marker['verts'], self.camera_intrinsics[0], self.camera_intrinsics[1], P=self.camera_intrinsics[0] )
            else:
                verts = np.zeros((4,1,2), dtype = np.float64)
                for i in range(4):
                    verts[i][0][0] = marker['verts'][i][0][0] + float(self.roi[0])
                    verts[i][0][1] = marker['verts'][i][0][1] + float(self.roi[1])

                verts = distortPoints(verts, self.camera_intrinsics[0], self.camera_intrinsics[1], new_cm=self.camera_intrinsics[4])
                verts = cv2.undistortPoints( verts, self.camera_intrinsics[0], self.camera_intrinsics[1], P=self.camera_intrinsics[0] )

            #deduce coordinate of the camera
            _, rvecs, tvecs = cv2.solvePnP(objp, verts, self.camera_intrinsics[0], None) #Already undistord, no need to give dist coeffs
            self.camera_coord = self.get_camera_coordinate(rvecs, tvecs)


        return marker
예제 #2
0
    def update(self,frame,events):
        self.img_shape = frame.height,frame.width,3

        if self.running:
            self.current_gaze_pos = self.get_gaze_pos(events)

            #drawing only in undistord image
            if self.show_undistord:
                self.frame_img = undistord_with_roi(img=frame.img, cm=self.camera_intrinsics[0], dist_coef=self.camera_intrinsics[1], roi=self.roi, new_cm=self.camera_intrinsics[4])
                gray = cv2.cvtColor(self.frame_img, cv2.COLOR_BGR2GRAY)
                cv2.imshow("test", self.frame_img)
            else:
                self.frame_img = frame.img
                gray = frame.gray


            if self.robust_detection:
                visible_markers = detect_markers_robust(gray,
                                                    grid_size = 5,
                                                    prev_markers=self.markers,
                                                    min_marker_perimeter=self.min_marker_perimeter,
                                                    aperture = 11, 
                                                    visualize=0,
                                                    true_detect_every_frame=3)
            else:
                visible_markers = detect_markers(gray,
                                                grid_size = 5,
                                                min_marker_perimeter=self.min_marker_perimeter,
                                                aperture = 11, 
                                                visualize=0)

            for m in self.markers:
                m['visible'] = False

            self.find_main_marker(visible_markers )

            for vm in visible_markers:
                #find the index of the visible marker in self.markers
                index = -1
                for indexList,m in enumerate(self.markers):
                    if m['id'] == vm['id']:
                        index = indexList
                        break

                if index == -1:  #marker is not registered already
                    index = len(self.markers)
                    new_marker = {'id':vm['id'],'verts':vm['verts'],'verts_norm':vm['verts_norm'],'centroid':vm['centroid'],'frames_since_true_detection':0,'height':76,'frames_looked_up_count':0,'obj_name':"None",'obj':None,'mult':1}
                    self.markers.append(new_marker)
                marker = self.markers[index]

                marker['verts'] = vm['verts']
                marker['verts_norm'] = vm['verts_norm']
                marker['centroid'] = vm['centroid']
                marker['frames_since_true_detection'] = vm['frames_since_true_detection']
                marker['visible'] = True
                objp = gen_square_pattern_grid(marker['height'])

                # Find the rotation and translation vectors.
                _, rvecs, tvecs = cv2.solvePnP(objp, marker['verts'], self.camera_intrinsics[0], None) #Already undistord, no need to give dist coeffs

                #if the marker is fixed by the gaze
                if self.is_looked_up(marker):
                    #get the obj to draw
                    if self.mode == "Draw obj":
                        if marker['obj'] == None and marker['obj_name'] != "None":
                            marker['obj'] = OBJ("../ressources/"+marker['obj_name'], marker['mult'], swapyz=True)

                        marker['rot'] = rvecs
                        marker['trans'] = tvecs
                        marker['to_draw'] = True

                else :  #not fixed
                    if self.mode == "Draw obj":
                       marker['to_draw'] = False

        if not self.running:
            self.button.status_text = 'tracking paused'