0% found this document useful (0 votes)
37 views3 pages

Codigos Contadora

This Python code defines a class called ObjectScreen that creates a GUI for object counting in images and video streams. It loads a UI file, connects buttons to methods for opening images and counting objects. It defines methods to open images, display them, and count objects by finding contours. For video, it defines a Work class with a thread to read frames, process them and emit updated images to the GUI with counted objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

Codigos Contadora

This Python code defines a class called ObjectScreen that creates a GUI for object counting in images and video streams. It loads a UI file, connects buttons to methods for opening images and counting objects. It defines methods to open images, display them, and count objects by finding contours. For video, it defines a Work class with a thread to read frames, process them and emit updated images to the GUI with counted objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import sys

from PyQt5 import QtGui, QtWidgets


from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
import cv2

class ObjectScreen(QMainWindow):
def __init__(self):
super(ObjectScreen, self).__init__()
loadUi("gui_conteo.ui", self)
self.filename = None
self.open_img.clicked.connect(self.cargarImagen)
self.contar.clicked.connect(self.contar_objetos)

def cargarImagen(self):
try:
self.filename = QFileDialog.getOpenFileName(filter="Image
(*.*)")[0]
imagen = cv2.imread(self.filename)
self.setPhoto(imagen)
except:
pass

def setPhoto(self, image):


frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
imagen = QImage(frame, frame.shape[1], frame.shape[0],
frame.strides[0], QImage.Format_RGB888)
imagen = imagen.scaled(321, 221, Qt.KeepAspectRatio)
self.label_img.setPixmap(QtGui.QPixmap.fromImage(imagen))

def contar_objetos(self):
umbral = 200
img = cv2.imread(self.filename)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, img_thres = cv2.threshold(img_gray, umbral, 255,
cv2.THRESH_BINARY_INV)
contornos, _ = cv2.findContours(img_thres, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
texto = str(len(contornos))
self.label_conta.setText(texto)

# main
app = QApplication(sys.argv)
welcome = ObjectScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(welcome)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Saliendo")
import sys
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QApplication, QMainWindow
import cv2
import numpy as np

class ObjectScreen(QMainWindow):
def __init__(self):
super(ObjectScreen, self).__init__()
loadUi("gui_conteo.ui", self)
self.filename = None
self.open_img.clicked.connect(self.start_video)

def start_video(self):
self.Work = Work()
self.Work.start()
self.Work.Imageupd.connect(self.Imageupd_slot)

def Imageupd_slot(self, Image):


self.label_img.setPixmap(QPixmap.fromImage(Image))
self.label_conta.setText(str(self.Work.contornos()))

class Work(QThread):
Imageupd = pyqtSignal(QImage)
def run(self):
self.hilo_corriendo = True
cap = cv2.VideoCapture(0)
while self.hilo_corriendo:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(gray, kernel)
dilation = cv2.dilate(erosion, kernel)
gauss = cv2.GaussianBlur(dilation, (5, 5), 0)
Canny = cv2.Canny(gauss, 100, 200)
self.ctns, _ = cv2.findContours(Canny, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(frame, self.ctns, -1, (0, 0, 255), 2)

if ret:
Image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
flip = cv2.flip(Image, 1)
convertir_QT = QImage(flip.data, flip.shape[1],
flip.shape[0], QImage.Format_RGB888)
pic = convertir_QT.scaled(480, 360, Qt.KeepAspectRatio)
self.Imageupd.emit(pic)

def contornos(self):
a = len(self.ctns)
return a

def stop(self):
self.hilo_corriendo = False
self.quit()
# main
app = QApplication(sys.argv)
welcome = ObjectScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(welcome)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Saliendo")

You might also like