-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
148 lines (112 loc) · 4.21 KB
/
main.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import cv2 as cv
import numpy as np
import math
import pyzbar.pyzbar as pyzbar
import betterLook
# Variable
camID = 0 # camera ID, or pass string as filename. to the camID
# Real world measured Distance and width of QR code
KNOWN_DISTANCE = 30.1 # inches
KNOWN_WIDTH = 5.0 # inches
# define the fonts
fonts = cv.FONT_HERSHEY_COMPLEX
Pos =(50,50)
# colors (BGR)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
MAGENTA = (255, 0, 255)
GREEN = (0, 255, 0)
CYAN = (255, 255, 0)
GOLD = (0, 255, 215)
YELLOW = (0, 255, 255)
ORANGE = (0, 165, 230)
# functions
# finding Distance between two points
def eucaldainDistance(x, y, x1, y1):
eucaldainDist = math.sqrt((x1 - x) ** 2 + (y1 - y) ** 2)
return eucaldainDist
# focal length finder function
def focalLengthFinder(knowDistance, knownWidth, widthInImage):
'''This function calculates the focal length. which is used to find the distance between object and camera
:param1 knownDistance(int/float) : it is Distance form object to camera measured in real world.
:param2 knownWidth(float): it is the real width of object, in real world
:param3 widthInImage(float): the width of object in the image, it will be in pixels.
return FocalLength(float): '''
focalLength = ((widthInImage * knowDistance) / knownWidth)
return focalLength
def distanceFinder(focalLength, knownWidth, widthInImage):
'''
This function basically estimate the distance, it takes the three arguments: focallength, knownwidth, widthInImage
:param1 focalLength: focal length found through another function .
param2 knownWidth : it is the width of object in the real world.
param3 width of object: the width of object in the image .
:returns the distance:
'''
distance = ((knownWidth * focalLength) / widthInImage)
return distance
def DetectQRcode(image):
codeWidth = 0
x, y = 0, 0
euclaDistance = 0
global Pos
# convert the color image to gray scale image
Gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# create QR code object
objectQRcode = pyzbar.decode(Gray)
for obDecoded in objectQRcode:
points = obDecoded.polygon
if len(points) > 4:
hull = cv.convexHull(
np.array([points for point in points], dtype=np.float32))
hull = list(map(tuple, np.squeeze(hull)))
else:
hull = points
n = len(hull)
# draw the lines on the QR code
for j in range(0, n):
# print(j, " ", (j + 1) % n, " ", n)
cv.line(image, hull[j], hull[(j + 1) % n], ORANGE, 2)
# finding width of QR code in the image
x, x1 = hull[0][0], hull[1][0]
y, y1 = hull[0][1], hull[1][1]
Pos = hull[3]
# using Eucaldain distance finder function to find the width
euclaDistance = eucaldainDistance(x, y, x1, y1)
# retruing the Eucaldain distance/ QR code width other words
return euclaDistance
# video recording
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('Demo.mp4', fourcc, 15.0, (640, 480))
# creating camera object
camera = cv.VideoCapture(camID)
refernceImage = cv.imread("referenceImage2.png")
# getting the width of QR code in the reference image
Rwidth= DetectQRcode(refernceImage)
# finding the focal length
focalLength = focalLengthFinder(KNOWN_DISTANCE, KNOWN_WIDTH, Rwidth)
print("Focal length: ", focalLengthFinder)
counter =0
while True:
ret, frame = camera.read()
# finding width of QR code width in the frame
codeWidth= DetectQRcode(frame)
# print(Value)
# print(codeWidth)
if codeWidth is not None:
# print("not none")
Distance = distanceFinder(focalLength, KNOWN_WIDTH, codeWidth)
# cv.putText(frame, f"Distance: {Distance}", (50,50), fonts, 0.6, (GOLD), 2)
betterLook.showText(frame, f"Distnace: {round(Distance,2)} Inches", Pos, GOLD, int(Distance * 4.5))
out.write(frame)
cv.imshow("frame", frame)
key = cv.waitKey(1)
if key == ord('s'):
# saving frame
counter += 1
print("frame saved")
cv.imwrite(f"frames/frame{counter}.png", frame)
if key == ord('q'):
break
camera.release()
cv.destroyAllWindows()
out.release()