-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
162 lines (102 loc) · 4.98 KB
/
Copy pathcamera.py
File metadata and controls
162 lines (102 loc) · 4.98 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QVBoxLayout, QPushButton, QProgressBar
from PyQt5.QtCore import QThread, Qt, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QImage, QPixmap
import cv2 as cv
import sys, serial, time
from array import array
face_cas = cv.CascadeClassifier('C:/Users/victo/AppData/Local/Programs/Python/Python311/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
serialPort = serial.Serial(port='COM3', baudrate=9600, timeout=.1)
def face(frame):
diffx = 0
diffy = 0
frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
frame_gray = cv.equalizeHist(frame_gray)
#-- Detect faces
faces = face_cas.detectMultiScale(frame_gray, 1.2, 5)
for (x,y,w,h) in faces:
center = array('i', [468, 384, 640, 480])
#1024, 768
#x: 185 x+w: 421 y:151 y+h: 387 center area of the screen
#pt1 = (x, y) #bottom left point
#pt2 = (x+w, y+h) # top left point
#pt3 = (x+w, y) # bottom right point
#pt4 = (x, y+h) # top left point
squareW = x+w
squareH = y+h
frame = cv.rectangle(frame, (x, y) , (squareW, squareH), (0, 255, 0), 2)
# print("x: " + str(x), "x+w: " + str(squareW), "y:" + str(y), "y+h: " + str(squareH))
diffx = (center[0] - squareW) # 320 - x+w
diffy = (center[1] - squareH) #240 - y+h
# creates the intial offset of the camera
offsetAngleX = 90
offsetAngleY = 90
offsetX = ((diffx/center[2] )) * 45 # finds how much the camera needs to move
offsetY = ((diffy/center[3] )) * 45
diffx = abs(offsetX) + offsetAngleX # ratio -> true horizontal movement value for servo
diffy = abs(offsetY) + offsetAngleY # ratio -> true vertical movement value for servo
diffx = int(diffx)
diffy = int(diffy)
print("x: " + str(diffx), "y: " + str(diffy))
if(diffx >=30 and diffx <= 150):
serialPort.write(f'X{diffx}\n'.encode())
# time.sleep(0.05)
if( diffy >=30 and diffy <= 150):
serialPort.write(f'Y{diffy}\n'.encode())
# time.sleep(0.05)
offsetAngleX += offsetX
offsetAngleY += offsetY
class Thread(QThread):
changePixmap = pyqtSignal(QImage)
def run(self):
cap = cv.VideoCapture(0)
print("Picture!")
while(1):
ret, frame = cap.read()
if ret:
face(frame)
rgbImage = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
h, w, ch = rgbImage.shape
bytesPerLine = ch * w
convertToQtFormat = QImage(rgbImage.data, w, h, bytesPerLine, QImage.Format_RGB888)
p = convertToQtFormat.scaled(1024, 768, Qt.KeepAspectRatio)
self.changePixmap.emit(p)
class CameraWidget(QWidget):
def __init__(self):
super().__init__()
self.title = 'Tracking-Camera'
self.left = 100
self.top = 100
self.width = 1024
self.height = 768
layout = QVBoxLayout(self)
self.label = QLabel(self)
layout.addWidget(self.label)
self.camera_button = QPushButton("Turn on camera", self)
self.initUI()
#self.bar = QProgressBar('Loading Camera', self)
#self.camera_button.clicked.connect(self.change_button)
self.camera_button.clicked.connect(self.initUI)
@pyqtSlot(QImage)
def setImage(self, image):
self.label.setPixmap(QPixmap.fromImage(image))
def change_button(self):
# if self.camera_button.isChecked:
self.camera_button.setEnabled(False)
self.camera_button.setStyleSheet('QPushButton:disabled { color: red }')
def initUI(self):
self.setWindowTitle( self.title)
self.setGeometry( self.left, self.top, self.width, self.height)
self.label = QLabel(self)
self.label.resize(1024, 768)
self.camera_button.resize(1024, 768)
th =Thread(self)
th.changePixmap.connect(self.setImage)
th.start()
self.show();
if __name__ == '__main__':
app = QApplication(sys.argv)
window = CameraWidget()
#window.show()
sys.exit(app.exec_())
#"C:/Users/victo/AppData/Local/Programs/Python/Python311/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml")
#"C:/Users/victo/AppData/Local/Programs/Python/Python311/Lib/site-packages/cv2/data/haarcascade_eye_tree_eyeglasses.xml")