Quantcast
Channel: Active questions tagged qpainter - Stack Overflow
Viewing all articles
Browse latest Browse all 58

question regarding drawing squares on uploaded image

$
0
0
import sysfrom PyQt5.QtGui import QPixmap, QPainter, QPen, QColor, QImagefrom PyQt5.QtWidgets import (    QApplication,    QWidget,    QPushButton,    QLabel,    QVBoxLayout,    QHBoxLayout,    QFileDialog,)from PyQt5.QtCore import Qt, QPoint, QRectimport cv2class MainWindow(QWidget):    def __init__(self):        super().__init__()        self.setGeometry(200, 200, 800, 600)        self.setWindowTitle("Image Display and Face Detection")        self.imagepath = ""        self.pixmap_resized = None        self.drawing = False        self.rect_start = QPoint()        self.rect_end = QPoint()        self.enable_drawing = False        self.label = QLabel("Image will be displayed here", self)        self.btn_upload = QPushButton("Upload Image", self)        self.btn_locate = QPushButton("Locate Faces", self)        self.btn_draw = QPushButton("Draw Rectangle", self)        self.btn_upload.clicked.connect(self.obtain_photo)        self.btn_locate.clicked.connect(self.find_face)        self.btn_draw.clicked.connect(self.enable_draw)        vbox = QVBoxLayout()        vbox.addWidget(self.label)        hbox = QHBoxLayout()        hbox.addWidget(self.btn_upload)        hbox.addWidget(self.btn_locate)        hbox.addWidget(self.btn_draw)        vbox.addLayout(hbox)        self.setLayout(vbox)    def obtain_photo(self):        fname = QFileDialog.getOpenFileName(self, "Open file")        if fname[0]:            self.imagepath = fname[0]            self.render_image()    def render_image(self):        self.pixmap = QPixmap(self.imagepath)        self.pixmap_resized = self.pixmap.scaled(            600, 600, Qt.KeepAspectRatio, Qt.SmoothTransformation        )        self.label.setPixmap(self.pixmap_resized)        self.update()    def display_image(self, img):        height, width, colors = img.shape        bytes_per_line = 3 * width        image = QImage(img.data, width, height, bytes_per_line, QImage.Format_RGB888)        self.pixmap_resized = QPixmap.fromImage(image.rgbSwapped())        self.update()    def find_face(self):        face_cascade = cv2.CascadeClassifier(            cv2.data.haarcascades +"haarcascade_frontalface_default.xml"        )        img = cv2.imread(self.imagepath, cv2.IMREAD_COLOR)        img = cv2.resize(img, (600, 600))        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)        faces = face_cascade.detectMultiScale(gray, 1.3, 5)        for x, y, w, h in faces:            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2)        self.display_image(img)    def enable_draw(self):        self.enable_drawing = True    def mousePressEvent(self, event):        if event.button() == Qt.LeftButton and self.enable_drawing:            self.drawing = True            self.rect_start = event.pos()            self.rect_end = self.rect_start            self.update()    def mouseMoveEvent(self, event):        if self.drawing and self.enable_drawing:            self.rect_end = event.pos()            self.update()    def mouseReleaseEvent(self, event):        if event.button() == Qt.LeftButton and self.enable_drawing:            self.drawing = False            self.rect_end = event.pos()            self.update()    def paintEvent(self, event):        super().paintEvent(event)        if self.pixmap_resized:            painter = QPainter(self)            painter.drawPixmap(0, 0, self.pixmap_resized)            if self.drawing and self.enable_drawing:                rect = QRect(self.rect_start, self.rect_end)                painter.setPen(QPen(QColor("blue"), 3))                painter.drawRect(rect.normalized())if __name__ == "__main__":    app = QApplication(sys.argv)    mainwin = MainWindow()    mainwin.show()    sys.exit(app.exec_())

after pressing the "upload image " button, i expected a singular image which i could discover, add, and remove faces from. However, what ended up occuring were two overlapping images, with one being able to have the faces removed and the other one being able to have faces added.

enter image description hereenter image description hereenter image description here

Dark blue square denotes how the faces are to be drawn.


Viewing all articles
Browse latest Browse all 58

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>