Python+Qt palmprint recognition

Featured program examples

Python+Qt palmprint recognition

If you need to install the operating environment or remote debugging, see the personal QQ business card at the bottom of the article, and professional and technical personnel will assist remotely!

foreword

This blog writes code for <<Python+Qt Palmprint Recognition>>, the code is neat, regular and easy to read. The first choice for learning and application recommendation.


Article directory

1. Required tool software

2. Use steps

        1. Import library

        2. Code implementation

       3. Running results

3. Online assistance

1. Required tool software

1. Python

2. Qt, OpenCV

2. Use steps

1. Import library

import cv2
import cv2 as cv
import numpy as np
from PyQt5 import QtWidgets

from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

2. Code implementation

code show as below:

class myWin(QtWidgets.QWidget, Ui_Dialog):

    def __init__(self):
        super(myWin, self).__init__()
        self.setupUi(self)

    def openFileButton(self):
        imgName, imgType  = QFileDialog.getOpenFileName(self,"打开文件","./","files(*.*)")
        img = cv2.imread(imgName)
        cv2.imwrite("temp/original.jpg", img)
        height, width, pixels = img.shape
        print("width,height",width,height)
        print("self.label.width()",self.label.width())
        print("self.label.height()",self.label.height())

        if width>(self.label.width()):
            rheight=(self.label.width()*height)*width
            rwidth=self.label.width()
            print("rwidth-if,rheight-if", width, rheight)
        elif height>(self.label.height()):
            rwidth=(self.label.height()*width)/height
            rheight=self.label.height()
            print("rwidth-elif,rheight-elfi", rwidth, rheight)
        elif ((self.label.height())-height)<((self.label.width())-width):
            rwidth=(self.label.height()*width)/height
            rheight=self.label.height()
            print("rwidth-elif,rheight-elfi", rwidth, rheight)
        else:
            print("rheight,rwidth", height, width)
            rheight = height
            rwidth = width

        frame = cv2.resize(img, (int(rwidth), int(rheight)))
        print("rwidth-elif,rheight-elfi", rwidth, rheight)
        img2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # opencv读取的bgr格式图片转换成rgb格式
        _image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3, QtGui.QImage.Format_RGB888)
        jpg_out = QtGui.QPixmap(_image).scaled(rwidth, rheight) #设置图片大小
        self.label.setPixmap(jpg_out) #设置图片显示


    def saveFileButton(self):
        img = cv2.imread("temp/original.jpg")
        file_path = QFileDialog.getSaveFileName(self, "save file", "./save/test","jpg files (*.jpg);;all files(*.*)")
        print(file_path[0])
        cv2.imwrite(file_path[0], img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()



    def fingerContrast(self):
        # 均值哈希算法
        def aHash(img):
            # 缩放为8*8
            img = cv2.resize(img, (8, 8), interpolation=cv2.INTER_CUBIC)
            # 转换为灰度图
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # s为像素和初值为0,hash_str为hash值初值为''
            s = 0
            hash_str = ''
            # 遍历累加求像素和
            for i in range(8):
                for j in range(8):
                    s = s + gray[i, j]

            # 灰度大于平均值为1相反为0生成图片的hash值
            for i in range(8):
                for j in range(8):
                    if gray[i, j] > avg:
                        hash_str = hash_str + '1'
                    else:
                        hash_str = hash_str + '0'
            return hash_str

        # 差值感知算法
        def dHash(img):
            # 缩放8*8
            img = cv2.resize(img, (9, 8), interpolation=cv2.INTER_CUBIC)
            # 转换灰度图
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            hash_str = ''
            # 每行前一个像素大于后一个像素为1,相反为0,生成哈希
            for i in range(8):
                for j in range(8):
                    if gray[i, j] > gray[i, j + 1]:
                        hash_str = hash_str + '1'
                    else:
                        hash_str = hash_str + '0'
            return hash_str

        # Hash值对比
        def cmpHash(hash1, hash2):
            n = 0
            # hash长度不同则返回-1代表传参出错
            if len(hash1) != len(hash2):
                return -1
            # 遍历判断
            for i in range(len(hash1)):
                # 不相等则n计数+1,n最终为相似度
                if hash1[i] != hash2[i]:
                    n = n + 1
            return n

        import os
        path = "palmDataBase/"
        file_list = os.listdir(path)
        for file in file_list:
            img1 = cv2.imread('temp/original.jpg')
            BasePath="palmDataBase/" + str(file)
            print("BasePath: ", BasePath)
            img2 = cv2.imread(BasePath)
            print("img2: ",img2)
            print(hash2)
            n = cmpHash(hash1, hash2)
            print('均值哈希算法相似度:' + str(n))


            print('差值哈希算法相似度:' + str(n))
            result='相似度:' + str(100-n)+", 通过"
            if n < 5:
                print("file:",file)
                self.textEdit.setPlainText(result)
                self.textEdit_2.setPlainText("匹配成功名称:"+file)
                print("n: ",n)

                if width > (self.label.width()):
                    rheight = (self.label.width() * height) * width
                    rwidth = self.label.width()
                    print("rwidth-if,rheight-if", width, rheight)
                elif height > (self.label.height()):
                    rwidth = (self.label.height() * width) / height
                    rheight = self.label.height()
                    print("rwidth-elif,rheight-elfi", rwidth, rheight)
                elif ((self.label.height()) - height) < ((self.label.width()) - width):
                    rwidth = (self.label.height() * width) / height
                    rheight = self.label.height()
                    print("rwidth-elif,rheight-elfi", rwidth, rheight)
                else:
                    print("rheight,rwidth", height, width)
                    rheight = height
                    rwidth = width

                frame = cv2.resize(img2, (int(rwidth), int(rheight)))
                print("rwidth-elif,rheight-elfi", rwidth, rheight)
                img2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # opencv读取的bgr格式图片转换成rgb格式
                _image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3,
                                      QtGui.QImage.Format_RGB888)
                jpg_out = QtGui.QPixmap(_image).scaled(rwidth, rheight)  # 设置图片大小
                self.label_2.setPixmap(jpg_out)  # 设置图片显示
                break
            else:
                print("n: ", n)
                self.textEdit.setPlainText("相似度太低,不通过")
                self.textEdit_2.setPlainText(" ")
                self.label_2.setPixmap(QPixmap(""))


if __name__=="__main__":

    app=QtWidgets.QApplication(sys.argv)
    Widget=myWin()
    Widget.showMaximized();
    Widget.show()
    sys.exit(app.exec_())

3. Running results

 

3. Online assistance:

If you need to install the operating environment or remote debugging, see the personal QQ business card at the bottom of the article, and professional and technical personnel will assist remotely!
1) Remote installation and operation environment, code debugging
2) Qt, C++, Python entry guide
3) Interface beautification
4) Software production

Blogger recommended article: python face recognition statistics qt form - CSDN Blog

Blogger recommended article: Python Yolov5 flame smoke recognition source code sharing - CSDN blog

                         Python OpenCV recognizes the number of people entering and exiting the pedestrian entrance - python recognizes the number of people - CSDN Blog

Personal blog homepage: alicema1111's blog_CSDN blog-Python, C++, bloggers in the field of web pages

Click here for all the blogger's articles: alicema1111's blog_CSDN blog-Python, C++, bloggers in the field of web pages

Guess you like

Origin blog.csdn.net/alicema1111/article/details/130423563