forked from rsarwar87/pyReflectorCollimator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpyCameraWindow.py
More file actions
executable file
·120 lines (98 loc) · 5.43 KB
/
pyCameraWindow.py
File metadata and controls
executable file
·120 lines (98 loc) · 5.43 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import dearpygui.dearpygui as dpg
import numpy as np
import cv2 as cv
import operator
class CameraWindow():
def __init__(self, cc):
self.cc = cc
ret, frame = cc.vid.read()
data = np.flip(frame, 2) # because the camera data comes in as BGR and we need RGB
data = data.ravel() # flatten camera data to a 1 d stricture
data = np.asfarray(data, dtype='f') # change data type to 32bit floats
self.texture_data = np.true_divide(data, 255.0) # normalize image data to prepare for GPU
print("texture_data Array:")
print("Array is of type: ", type(self.texture_data))
print("No. of dimensions: ", self.texture_data.ndim)
print("Shape of array: ", self.texture_data.shape)
print("Size of array: ", self.texture_data.size)
print("Array stores elements of type: ", self.texture_data.dtype)
with dpg.texture_registry():
dpg.add_raw_texture(frame.shape[1], frame.shape[0],
self.texture_data, tag="texture_tag",
format=dpg.mvFormat_Float_rgb)
with dpg.window(label="Capture Window",pos=(500, 0),
no_close=True) as self.mainForm:
dpg.add_image("texture_tag")
def rotate(self, p, origin=(0, 0), degrees=0):
angle = np.deg2rad(degrees)
R = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
o = np.atleast_2d(origin)
p = np.atleast_2d(p)
return np.squeeze((R @ (p.T-o.T) + o.T).T)
def update_frame(self):
# updating the texture in a while loop the frame rate will be limited to the camera frame rate.
# commenting out the "ret, frame = vid.read()" line will show the full speed that operations and updating a texture can run at
ret, frame = self.cc.vid.read()
cc = self.cc
if (cc.zoom > 0):
height, width, channels = frame.shape
centerX,centerY=int(height/2),int(width/2)
scale = cc.zoom/100
radiusX,radiusY= int(scale*height/2),int(scale*width/2)
minX,maxX=centerX-radiusX,centerX+radiusX
minY,maxY=centerY-radiusY,centerY+radiusY
cropped = frame[minX:maxX, minY:maxY]
frame = cv.resize(cropped, (width, height))
if cc.cbOffset == False:
origin = (int(cc.frame_width/2), int(cc.frame_height/2))
else:
origin = (int(cc.frame_width/2 + (cc.frame_width/2 - cc.xoffset)/10), int(cc.frame_height/2 + (cc.frame_height/2 - cc.yoffset)/2 ))
if cc.roll > 0:
M = cv.getRotationMatrix2D(origin, cc.roll, 1)
frame = cv.warpAffine(frame, M, (int(cc.frame_width), int(cc.frame_height)))
if cc.tilt != 50:
tratio_w = int(((cc.tilt - 50)/500)*cc.frame_width)
tratio_h = int(((cc.tilt - 50)/500)*cc.frame_height)
iframe = np.float32([[tratio_w,tratio_h], [cc.frame_width-tratio_w-1, tratio_h],
[cc.frame_width +tratio_w- 1, cc.frame_height+tratio_h-1],
[-tratio_w, cc.frame_height +tratio_h- 1]])
oframe = np.float32([[0,0], [cc.frame_width-1, 0],
[cc.frame_width - 1, cc.frame_height-1],
[0, cc.frame_height - 1]])
M = cv.getPerspectiveTransform(iframe, oframe)
frame = cv.warpPerspective(frame, M, (int(cc.frame_width), int(cc.frame_height)),
cv.INTER_LINEAR, borderMode=cv.BORDER_CONSTANT, borderValue=(0,0,0))
for i in range(0, 3):
if cc.cbCircleEnable[i] is True:
j = 1
cv.circle(frame, origin,
radius = cc.cbRadius[i],
color = (cc.cbRBG[i][2]*j, cc.cbRBG[i][1]*j, cc.cbRBG[i][0]*j),
thickness = cc.cbThickness[i])
for i in range(0, 2):
if cc.cbCrossEnable[i] is True:
j = 1
points = [(int(origin[0] - cc.cbLength[i]/2 ), origin[1]),
(int(origin[0] + cc.cbLength[i]/2 ), origin[1])]
rp = self.rotate(points, origin=origin, degrees=cc.cbAngle[i])
cv.line(frame, (int(rp[0][0] ), int(rp[0][1])),
(int(rp[1][0] ), int(rp[1][1])),
color = (cc.cbRBG[i+3][2]*j, cc.cbRBG[i+3][1]*j, cc.cbRBG[i+3][0]*j),
thickness = cc.cbThickness[i+3])
points = [(int(origin[0]), int(origin[1] - cc.cbLength[i]/2)),
(origin[0], int(origin[1] + cc.cbLength[i]/2))]
rp = self.rotate(points, origin=origin, degrees=cc.cbAngle[i])
cv.line(frame, (int(rp[0][0] ), int(rp[0][1])),
(int(rp[1][0] ), int(rp[1][1])),
color = (cc.cbRBG[i+3][2]*j, cc.cbRBG[i+3][1]*j, cc.cbRBG[i+3][0]*j),
thickness = cc.cbThickness[i+3])
data = np.flip(frame, 2)
data = data.ravel()
data = np.asfarray(data, dtype='f')
self.texture_data = np.true_divide(data, 255.0)
dpg.set_value("texture_tag", self.texture_data)
# to compare to the base example in the open cv tutorials uncomment below
#cv.imshow('frame', frame)