forked from Sooner-Rover-Team/Autonomous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findFocalLength.py
executable file
·65 lines (51 loc) · 2.12 KB
/
findFocalLength.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
#!/usr/bin/python3
#This programs returns the camera focal length assuming tag is tag 0, 20cm wide and 100cm away
import cv2
import cv2.aruco as aruco
import numpy as np
import os
import configparser
from time import sleep
#opens the camera
cam = cv2.VideoCapture('/dev/video0')
if not cam.isOpened():
print("Camera not connected")
exit(-1)
#sets up the config stuff
config = configparser.ConfigParser(allow_no_value=True)
config.read(os.path.dirname(__file__) + '/config.ini')
#sets the resolution and 4cc
format = config['ARTRACKER']['FORMAT']
cam.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(format[0], format[1], format[2], format[3]))
dppH = float(config['ARTRACKER']['DEGREES_PER_PIXEL'])
dppV = float(config['ARTRACKER']['VDEGREES_PER_PIXEL'])
width = int(config['ARTRACKER']['FRAME_WIDTH'])
height = int(config['ARTRACKER']['FRAME_HEIGHT'])
cam.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
tagDict = aruco.Dictionary_get(aruco.DICT_4X4_50)
while True:
try:
#takes image and detects markers
ret, image = cam.read()
corners, markerIDs, rejected = aruco.detectMarkers(image, tagDict)
tagWidth = 0
if not markerIDs is None and markerIDs[0] == 4:
tagWidth = corners[0][0][1][0] - corners[0][0][0][0]
#IMPORTANT: Assumes tag is 20cm wide and 100cm away
focalLength = (tagWidth * 100) / 20
print("Focal length: ", focalLength)
centerXMarker = (corners[0][0][1][0] + corners[0][0][0][0]) / 2
angleToMarkerH = dppH * (centerXMarker - width/2)
print('Horizontal angle: ', angleToMarkerH)
centerYMarker = (corners[0][0][0][1] + corners[0][0][2][1]) / 2
angleToMarkerV = -1 *dppV * (centerYMarker - height/2)
print('Vertical angle: ', angleToMarkerV)
else:
print("Nothing found")
sleep(.1)
except KeyboardInterrupt:
break
except:
pass #When running multiple times in a row, must cycle through a few times
cam.release() #releases the camera