Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lildutchie99 committed Feb 24, 2020
0 parents commit 94acd85
Show file tree
Hide file tree
Showing 23 changed files with 234 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ROB 464 P1 - Red Team 2020
Binary file added calib.npz
Binary file not shown.
Binary file added calibration/Photo on 2-24-20 at 5.15 PM.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.18 PM.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.19 PM #2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.19 PM #3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.19 PM #4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.19 PM.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.20 PM #2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.20 PM #3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.20 PM #4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.20 PM #5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.20 PM.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.21 PM #2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.22 PM #2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibration/Photo on 2-24-20 at 5.22 PM.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions camera_cal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np
import cv2 as cv
import glob

# termination criteria
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = glob.glob('calibration/*.jpg')
for fname in images:
img = cv.imread(fname)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv.findChessboardCorners(gray, (7,6), None)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria)
print(corners2)
imgpoints.append(corners)
# Draw and display the corners
cv.drawChessboardCorners(img, (7,6), corners2, ret)
cv.imshow('img', img)
cv.waitKey(500)

cv.destroyAllWindows()

ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)

np.savez('calib.npz', mtx=mtx, dist=dist, rvecs=rvecs, tvecs=tvecs)
Binary file added checkerboard.pdf
Binary file not shown.
Binary file added measurements.xlsx
Binary file not shown.
Binary file added qrs.docx
Binary file not shown.
Binary file added qrs.pdf
Binary file not shown.
115 changes: 115 additions & 0 deletions webcam_qr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 7 11:41:42 2018
@author: Caihao.Cui
"""
from __future__ import print_function

import pyzbar.pyzbar as pyzbar
import numpy as np
import cv2
import time

# get the webcam:
cap = cv2.VideoCapture(0)

cap.set(3,1920)
cap.set(4,1080)
#160.0 x 120.0
#176.0 x 144.0
#320.0 x 240.0
#352.0 x 288.0
#640.0 x 480.0
#1024.0 x 768.0
#1280.0 x 1024.0
time.sleep(2)

def decode(im) :
# Find barcodes and QR codes
decodedObjects = pyzbar.decode(im)
# Print results
for obj in decodedObjects:
print('Type : ', obj.type)
print('Data : ', obj.data,'\n')
return decodedObjects


font = cv2.FONT_HERSHEY_SIMPLEX

#diagnal
#cal_x = [306, 364, 450, 610, 890]

cal_x = np.array([212, 254, 312, 420, 622])
cal_y = np.array([30, 25, 20, 15, 10])

cal_xp = np.array([220, 506, 940, 1374, 1700])
cal_yp = np.array([36, 13, 0, 13, 36])

while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
im = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

decodedObjects = decode(im)

for decodedObject in decodedObjects:
points = decodedObject.polygon

# If the points do not form a quad, find convex hull
if len(points) > 4 :
hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
hull = list(map(tuple, np.squeeze(hull)))
else :
hull = points;

cx = (hull[0][0] + hull[2][0]) / 2
cy = (hull[0][1] + hull[2][1]) / 2
frame = cv2.circle(frame, (int(cx), int(cy)), 10, (0, 255, 255), thickness=5)
print(cx, cy)

# Number of points in the convex hull
n = len(hull)
# Draw the convext hull
for j in range(0,n):
cv2.line(frame, hull[j], hull[ (j+1) % n], (255,0,0), 3)

x = decodedObject.rect.left
y = decodedObject.rect.top

#pdist = np.sqrt(decodedObject.rect.width**2 + decodedObject.rect.height**2)
pdist = decodedObject.rect.height
print("height", pdist)

xc = decodedObject.rect.left + decodedObject.rect.width/2
cfac = np.interp(xc, cal_xp, cal_yp)
print("correction amount", cfac)

pdist -= cfac
print("corrected height", pdist)
print("x", xc)

rdist = np.interp(pdist, cal_x, cal_y)
print("actual distance", rdist, " cm")

print(x, y)

print('Type : ', decodedObject.type)
print('Data : ', decodedObject.data,'\n')

barCode = str(decodedObject.data)
cv2.putText(frame, barCode, (x, y), font, 1, (0,255,255), 2, cv2.LINE_AA)

# Display the resulting frame
frame = cv2.resize(frame, (640, 360))
cv2.imshow('frame',frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
elif key & 0xFF == ord('s'): # wait for 's' key to save
cv2.imwrite('Capture.png', frame)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
84 changes: 84 additions & 0 deletions webcam_qr_perspective.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from __future__ import print_function

import pyzbar.pyzbar as pyzbar
import numpy as np
import cv2
import time
import glob

with np.load('calib.npz') as X:
mtx, dist, _, _ = [X[i] for i in ('mtx','dist','rvecs','tvecs')]
objp = np.array([[1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 0]], dtype=np.float32)

# get the webcam:
cap = cv2.VideoCapture(0)

cap.set(3,1920)
cap.set(4,1080)
time.sleep(2)

def decode(im) :
# Find barcodes and QR codes
decodedObjects = pyzbar.decode(im)
# Print results
for obj in decodedObjects:
print('Type : ', obj.type)
print('Data : ', obj.data,'\n')
return decodedObjects


font = cv2.FONT_HERSHEY_SIMPLEX

while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
im = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

decodedObjects = decode(im)

for decodedObject in decodedObjects:
points = decodedObject.polygon

# If the points do not form a quad, find convex hull
if len(points) > 4 :
hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
hull = list(map(tuple, np.squeeze(hull)))
else :
hull = points;

# Number of points in the convex hull
n = len(hull)

if n == 4:
cx = (hull[0].x + hull[2].x) / 2
cy = (hull[0].y + hull[2].y) / 2
frame = cv2.circle(frame, (int(cx), int(cy)), 5, (0, 0, 0))
hull.sort(key=lambda p: np.arctan2(p.y - cy, p.x - cx)) #sort counterclockwise
print(hull)
ret, rvecs, tvecs = cv2.solvePnP(objp, np.array(hull, dtype=np.float64), mtx, dist)
print(tvecs)

# Draw the convext hull
for j in range(0,n):
cv2.line(frame, hull[j], hull[ (j+1) % n], (255,0,0), 3)
cv2.line(frame, hull[1], hull[2], (0,0,255), 3)

print('Type : ', decodedObject.type)
print('Data : ', decodedObject.data,'\n')

#barCode = str(decodedObject.data)
#cv2.putText(frame, barCode, (x, y), font, 1, (0,255,255), 2, cv2.LINE_AA)

# Display the resulting frame
frame = cv2.resize(frame, (640, 360))
cv2.imshow('frame',frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
elif key & 0xFF == ord('s'): # wait for 's' key to save
cv2.imwrite('Capture.png', frame)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

0 comments on commit 94acd85

Please sign in to comment.