-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38-Augmented reality applications(not working).py
More file actions
61 lines (49 loc) · 2.04 KB
/
Copy path38-Augmented reality applications(not working).py
File metadata and controls
61 lines (49 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import cv2
import numpy as np
# Load the 3D cube model
cube_model = np.array([[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, -1],
[1, 0, -1],
[1, 1, -1],
[0, 1, -1]], dtype=np.float32)
# Load the camera intrinsic parameters
focal_length_x = 500 # Focal length along the x-axis (in pixels)
focal_length_y = 500 # Focal length along the y-axis (in pixels)
image_width = 640 # Image width (in pixels)
image_height = 480 # Image height (in pixels)
camera_matrix = np.array([[focal_length_x, 0, image_width / 2],
[0, focal_length_y, image_height / 2],
[0, 0, 1]], dtype=np.float32)
# Define the detected corners in the image
detected_corners = np.array([[100, 100],
[200, 100],
[200, 200],
[100, 200]], dtype=np.float32)
# Initialize the video capture
video = cv2.VideoCapture(0)
while True:
# Read a frame from the video capture
ret, frame = video.read()
if not ret:
break
# Perform camera calibration and pose estimation
_, rvec, tvec, _ = cv2.solvePnPRansac(cube_model, detected_corners, camera_matrix, None)
# Project the 3D model onto the frame
cube_points, _ = cv2.projectPoints(cube_model, rvec, tvec, camera_matrix, None)
cube_points = np.int32(cube_points).reshape(-1, 2)
# Draw the cube onto the frame
cv2.drawContours(frame, [cube_points[:4]], -1, (0, 255, 0), 2)
for i in range(4):
cv2.line(frame, tuple(cube_points[i]), tuple(cube_points[i + 4]), (0, 255, 0), 2)
cv2.drawContours(frame, [cube_points[4:]], -1, (0, 255, 0), 2)
# Display the augmented reality frame
cv2.imshow('Augmented Reality', frame)
# Exit if the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture and destroy windows
video.release()
cv2.destroyAllWindows()