-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcamQT.py
96 lines (79 loc) · 3.03 KB
/
webcamQT.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
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
import sys
import cv2
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import QTimer, Qt, pyqtSignal, QPoint, QRect
from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen, QColor
class ImageLabel(QLabel):
doubleClicked = pyqtSignal()
dragPosition = pyqtSignal(QPoint, QPoint)
def __init__(self):
super().__init__()
self.originalPixmap = None
self.dragStartPos = None
self.currentRect = None
self.doubleClickPos = None
def setOriginalPixmap(self, pixmap):
self.originalPixmap = pixmap
self.setPixmap(pixmap)
def mouseDoubleClickEvent(self, event):
self.currentRect = None
self.doubleClickPos = event.pos()
self.doubleClicked.emit()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.dragStartPos = event.pos()
def mouseMoveEvent(self, event):
if not self.dragStartPos:
return
self.currentRect = QRect(self.dragStartPos, event.pos())
self.update()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.dragStartPos:
self.dragPosition.emit(self.dragStartPos, event.pos())
self.dragStartPos = None
def paintEvent(self, event):
super().paintEvent(event)
if not self.currentRect:
return
painter = QPainter(self)
painter.drawPixmap(self.rect(), self.originalPixmap)
pen = QPen(QColor(255, 0, 0), 2)
painter.setPen(pen)
painter.drawRect(self.currentRect)
class WebcamViewer(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.layout = QVBoxLayout()
self.imageLabel = ImageLabel()
self.layout.addWidget(self.imageLabel)
self.setLayout(self.layout)
# Setup the webcam feed
self.capture = cv2.VideoCapture(0)
self.timer = QTimer(self)
self.timer.timeout.connect(self.updateFrame)
self.timer.start(30)
# Connect signals
self.imageLabel.doubleClicked.connect(self.onDoubleClick)
self.imageLabel.dragPosition.connect(self.onDragPosition)
def updateFrame(self):
ret, frame = self.capture.read()
if ret:
image = QImage(frame, frame.shape[1], frame.shape[0],
frame.strides[0], QImage.Format_BGR888)
pixmap = QPixmap.fromImage(image)
self.imageLabel.setOriginalPixmap(pixmap)
def onDoubleClick(self):
print("Double Clicked")
# return the clicked mouse image coordinates
return self.imageLabel.doubleClickPos.getCoords()
def onDragPosition(self, start, end):
print(f"Dragged from {start} to {end}")
# Convert to image coordinates and handle the event
return self.imageLabel.currentRect.getCoords()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = WebcamViewer()
ex.show()
sys.exit(app.exec_())