def main(self): ''' Screencapture from the webcam frame to get the marker pattern ''' cap = cv2.VideoCapture(0) if self.mode == 'capture': while True: # Capture frame-by-frame ret, frame = cap.read() currentFrame = frame.copy() cv2.namedWindow("choose marker") cv2.setMouseCallback("choose marker", self.click_and_crop) # Display the resulting frame cv2.imshow('choose marker',frame) # if there are two reference points, then crop the region of interest # from teh image and display it if len(self.referencePoints) == 2: cropImage = currentFrame[self.referencePoints[0][1]:self.referencePoints[1][1], \ self.referencePoints[0][0]:self.referencePoints[1][0]] cv2.rectangle(currentFrame, self.referencePoints[0], self.referencePoints[1], (0, 255, 0), 2) # initialize a marker object for the marker self.roi = ROI(cropImage, self.alg) cv2.imshow('choose marker',currentFrame) cv2.waitKey(1000) cv2.destroyWindow('choose marker') break if cv2.waitKey(1) & 0xFF == ord('q'): cap.release() cv2.destroyAllWindows() break else: roi = cv2.imread('roi.png') self.roi = ROI(roi, self.alg) ''' Handling the logic for marker pattern matching ''' cv2.waitKey(100) matcher = Matcher(self.roi, self.alg, disCoeff, cameraMatrix) while True: # Capture frame-by-frame ret, frame = cap.read() currentFrame = frame.copy() mirrorFrame = cv2.resize(frame, (0,0), fx=0.3, fy=0.3) ''' plt.subplot(2,1,1),plt.imshow(self.roi.image) plt.subplot(2,1,2),plt.imshow(currentFrame) plt.show() ''' cv2.namedWindow('webcam') cv2.imshow('webcam', currentFrame) matcher.setFrame(currentFrame) result = matcher.getCorrespondence() if result: # get the corners (src, dst, corners) = result else: # Not enough matching points found print('Not enough points') cv2.waitKey(1) continue (retvalCorner, rvecCorner, tvecCorner) = matcher.computePose(self.roi.getPoints3d(), corners) if retvalCorner: # Set up where to draw the axises of the the cube in the frame axis = np.float32([[0,0,0], [0,1,0], [1,1,0], [1,0,0], [0,0,-1],[0,1,-1],[1,1,-1],[1,0,-1] ]) ''' cv2.projectPoints(objectPoints, rvec, tvec, cameraMatrix, distCoeffs [, imagePoints[, jacobian[, aspectRatio]]]) → imagePoints, jacobian ''' # project 3d points to 2d coordinates in the frame coordination imgpts, jac = cv2.projectPoints(axis, rvecCorner, tvecCorner, cameraMatrix, disCoeff) # re-draw the frame currentFrame = cv2.polylines(currentFrame,[np.int32(corners)],True,255,3, cv2.LINE_AA) currentFrame = self.renderImg(currentFrame, imgpts) cv2.imshow('webcam', currentFrame) #plt.subplot(2,1,2),plt.imshow(currentFrame) #plt.show() cv2.waitKey(1) else: #print('not able to solve pnp') cv2.waitKey(1) continue if cv2.waitKey(1) & 0xFF == ord('q'): cap.release() cv2.destroyAllWindows() break