Skip to content

Instantly share code, notes, and snippets.

@Kyu
Created December 29, 2022 20:54
Show Gist options
  • Save Kyu/7b0d464efa87718f41ff91c1f9c98302 to your computer and use it in GitHub Desktop.
Save Kyu/7b0d464efa87718f41ff91c1f9c98302 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
def read_camera_parameters():
cam_mtx = np.loadtxt("cam_mtx.dat")
dist_params = np.loadtxt("dist_params.dat")
# cmtx = camera matrix, dist = distortion parameters
return cam_mtx, dist_params
def get_qr_coords(cam_mat, dist_para, points):
qr_edges = np.array(
[
[0, 0, 0],
[0, 1, 0],
[1, 1, 0],
[1, 0, 0]
],
dtype='float32'
).reshape((4, 1, 3))
ret, rvec, tvec = cv2.solvePnP(qr_edges, points, cam_mat, dist_para)
unitv_points = np.array(
[
[0, 0, 0],
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
],
dtype='float32'
).reshape((4, 1, 3))
if ret:
points, jac = cv2.projectPoints(unitv_points, rvec, tvec, cam_mat, dist_para)
return points, rvec, tvec
return [], [], []
def main():
capture = cv2.VideoCapture(input_src)
qr = cv2.QRCodeDetector()
# cv2.namedWindow("Frame", cv2.WINDOW_NORMAL)
while capture.isOpened():
ret, frame = capture.read()
if not ret:
break
value, points, qr_matrix = qr.detectAndDecode(frame)
if value:
print(value, points)
key = cv2.waitKey(1)
if key == ord('q') or key == 27:
break
if value:
axis_points, rvec, tvec = get_qr_coords(cmtx, dist, points)
# rvec, jacobian = cv2.Rodrigues(rvec)
# camera_position = -rvec.transpose() @ tvec
# BGR formatted colors
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 0, 0)]
if len(axis_points):
axis_points = axis_points.reshape((4, 2))
origin = (int(axis_points[0][0]), int(axis_points[0][1]))
for p, c in zip(axis_points[1:], colors[:3]):
p = (int(p[0]), int(p[1]))
# Sometimes qr detector will make a mistake and projected point will overflow integer value.
# We skip these cases.
if origin[0] > 5 * frame.shape[1] or origin[1] > 5 * frame.shape[1]:
break
if p[0] > 5 * frame.shape[1] or p[1] > 5 * frame.shape[1]:
break
cv2.line(frame, origin, p, c, 5)
cv2.putText(frame, f"{value}", origin, cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 5,
cv2.LINE_AA)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == ord('q') or key == 27:
break
capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
input_src = "rstp://192.168.1.167:31332/1"
print("!")
cmtx, dist = read_camera_parameters()
main()
import socket
import sys
import cv2
import pickle
import numpy as np
HOST = 'http://192.168.1.167:4747/video'
cap = cv2.VideoCapture("http://192.168.1.167:4747/video")
print(cv2.getBuildInformation())
# cap.open(HOST)
print(cap.isOpened())
while True:
ret, frame = cap.read()
cv2.imshow('s', frame)
if cv2.waitKey(1) == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment