0% found this document useful (0 votes)
23 views6 pages

adsasd

Uploaded by

yenac55419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

adsasd

Uploaded by

yenac55419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

import os

import sys
import cv2

from PyQt5.QtGui import QPixmap, QImage


from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel,
QVBoxLayout, QHBoxLayout, QFileDialog, \
QLineEdit, QRadioButton, QMessageBox
from PIL import Image

# Inherit from QWidget


class MainWindow(QWidget):
def __init__(self):
super().__init__()

# Window title
self.setWindowTitle('Face Tagging')

# Window position & size


self.top = 200
self.left = 500
self.width = 400
self.height = 400
self.setGeometry(self.left, self.top, self.width, self.height)

# Image dimensions
self.imgwidth = 300
self.imgheight = 400

# Widgets
def setWidgets(self):
self.btn1 = QPushButton('Upload image', self)
self.btn1.clicked.connect(self.getImagePath)
self.btn2 = QPushButton('Edit image', self)
self.btn2.clicked.connect(self.createEditingWindow)
self.btn3 = QPushButton('Tag faces', self)
self.btn3.clicked.connect(self.findFace)
self.btn4 = QPushButton('4', self)
self.btn5 = QPushButton('5', self)
self.btn6 = QPushButton('6', self)

# Image area
self.label = QLabel('Your image will show up here.\nONLY JPEG IS
SUPPORTED.', self)

# vbox with buttons


vbox = QVBoxLayout()
vbox.addWidget(self.btn2)
vbox.addWidget(self.btn3)
vbox.addWidget(self.btn4)
vbox.addWidget(self.btn5)
vbox.addWidget(self.btn6)

# vbox as a widget
buttons_widget = QWidget()
buttons_widget.setLayout(vbox)

# hbox with label & vbox


hbox = QHBoxLayout()
hbox.addWidget(self.label)
hbox.addWidget(buttons_widget)

# hbox as a widget
t = QWidget()
t.setLayout(hbox)

# vbox2 with hbox & upload button


vbox2 = QVBoxLayout()
vbox2.addWidget(t)
vbox2.addWidget(self.btn1)

# Window layout
self.setLayout(vbox2)

# Get image path


def getImagePath(self):
# File info
fname = QFileDialog.getOpenFileName(self, 'Open file')

# Loaded image path


self.imagepath = fname[0]

# Original image path


self.originalpath = fname[0]

self.loadImage()

# Show image
def loadImage(self):
# Image as a QPixmap object
self.pixmap = QPixmap(self.imagepath)

# Set image size


pixmap_resized = self.pixmap.scaled(win.imgwidth, win.imgheight)

# Image on the label


self.label.setPixmap(pixmap_resized)

# New EditWindow object


def createEditingWindow(self):
self.editwin = EditWindow()
self.editwin.setWidgets()
self.editwin.show()

def findFace(self):
self.fList = FaceList()

# Load the XML file for Haar Cascade


face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
try:
img = cv2. imread(self.imagepath, cv2. IMREAD_COLOR)
except AttributeError:
QMessageBox.question(self, 'Error', 'Upload an image first.',
QMessageBox.Ok)

img = cv2.resize(img, (self.imgwidth, self.imgheight))


# Convert to greyscale for Haar Cascade
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Parameter 1 : image
# Parameter 2 : scale factor; high/low factor for faster/precise processing
# Parameter 3 : high/low for rigorous/lenient processing
# faces is the list of (x, y, w, h)'s - the top left coordinates, rectangle
width, and height, resp.
faces = face_cascade.detectMultiScale(grey, 1.2, 1) # .tolist()

cnt = 0
for (x, y, w, h) in faces:
# Logging in the terminal
# print("Tagged :", x, y, w, h)
print("Tagged :", x + w//2, y + h//2, (w+h)//4)

# (x, y, w, h) to a Face object


self.fList.append_face(x, y, w, h)

# Drawing using OpenCV


# Parameters : image, top-left (int, int), bottom-right (int, int),
colour (BGR), thickness
# cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

# Parameters : image, centre, radius, colour (BGR), thickness


cv2.circle(img, (x + w//2, y + h//2), (w+h)//4, (114, 0, 51), 2)

cnt += 1
self.showImage(img)

def showImage(self, img):


height = img.shape[0]
width = img.shape[1]
colours = img.shape[2]

# Colour depth of JPEG is 8 bits ... each pixel is 8 * 3 bits = 3 bytes


# (R, G, B)
bytesPerLine = 3 * width

# OpenCV image to QImage


image = QImage(img.data, width, height, bytesPerLine, QImage.Format_RGB888)

# RGB to BGR
self.image = image.rgbSwapped()

self.label.setPixmap(QPixmap.fromImage(self.image))

# Inherit from QWidget


class EditWindow(QWidget):
def __init__(self):
super().__init__()
self.width = 200
self.height = 300
self.top = 200
self.left = 500
self.setGeometry(self.left, self.top, self.width, self.height)

def setWidgets(self):
self.labelwidth = QLabel('Set width')
self.textwidth = QLineEdit(str(win.imgwidth), self)
self.labelheight = QLabel('Set height')
self.textheight = QLineEdit(str(win.imgheight), self)
self.labelcolour = QLabel('Recolour')

# Radio buttons for colour options


self.radiobtn1 = QRadioButton('Original')

# Checked by default
self.radiobtn1.setChecked(True)
self.radiochecked = 'Original'
self.radiobtn2 = QRadioButton('Greyscale')
self.radiobtn3 = QRadioButton('R')
self.radiobtn4 = QRadioButton('G')
self.radiobtn5 = QRadioButton('B')

# Call 'btnstate' when toggled


self.radiobtn1.toggled.connect(self.btnstate)
self.radiobtn2.toggled.connect(self.btnstate)
self.radiobtn3.toggled.connect(self.btnstate)
self.radiobtn4.toggled.connect(self.btnstate)
self.radiobtn5.toggled.connect(self.btnstate)

# Apply button
self.btnOK = QPushButton('Apply', self)
self.btnOK.clicked.connect(lambda: self.editImage())

# The ultimate vbox


vbox = QVBoxLayout()
vbox.addWidget(self.labelwidth)
vbox.addWidget(self.textwidth)
vbox.addWidget(self.labelheight)
vbox.addWidget(self.textheight)
vbox.addWidget(self.labelcolour)
vbox.addWidget(self.radiobtn1)
vbox.addWidget(self.radiobtn2)
vbox.addWidget(self.radiobtn3)
vbox.addWidget(self.radiobtn4)
vbox.addWidget(self.radiobtn5)
vbox.addWidget(self.btnOK)

# Set layout
self.setLayout(vbox)

def btnstate(self):
# sender() is the toggled radio button
radiobtn = self.sender()
self.radiochecked = radiobtn.text()

def editImage(self):
# Fetch from the input fields
imgwidth_edited = self.textwidth.text()
imgheight_edited = self.textheight.text()
try:
win.imgwidth = int(imgwidth_edited)
win.imgheight = int(imgheight_edited)

# Here, 'Image' is an inherited class from the library 'Pillow'


img = Image.open(win.originalpath) # TODO ADD TRY FOR NO UPLOAD

if self.radiochecked == 'Original':
img_edited = img
if self.radiochecked == 'Greyscale':
# 'L' is predefined for greyscale
img_edited = img.convert('L')
if self.radiochecked == 'R':
red = (
0.90, 0.36, 0.18, 0,
0.11, 0.72, 0.07, 0,
0.02, 0.12, 0.95, 0)
img_edited = img.convert('RGB', red)
if self.radiochecked == 'G':
green = (
0.41, 0.36, 0.18, 0,
0.50, 0.72, 0.07, 0,
0.02, 0.12, 0.95, 0)
img_edited = img.convert('RGB', green)
if self.radiochecked == 'B':
blue = (
0.31, 0.36, 0.18, 0,
0.40, 0.72, 0.07, 0,
0.60, 0.12, 0.95, 0)
img_edited = img.convert('RGB', blue)

# Save edited image


img_edited.save('image_edited.jpg', 'JPEG')

# os.getcwd() returns the folder of this Python file


win.imagepath = os.getcwd() + '/image_edited.jpg'

win.loadImage()

# Reload image
win.loadImage()

# Close editing window


self.close()
except ValueError:
QMessageBox.question(self, 'Error', 'Check dimensions & image format
(JPEG)', QMessageBox.Ok)

class Face:
def __init__(self, x, y, w, h, name, idx):
self.x = x
self.y = y
self.w = w
self.h = h
self.name = name
self.idx = idx

class FaceList:
def __init__(self):
self.face_list = []
self.next_idx = 0
def append_face(self, x, y, w, h):
self. face_list.append(Face(x, y, w, h, "", self.next_idx))
self.next_idx += 1

# Main
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.setWidgets()

win.show()
app.exec_()

You might also like