Face recognition study notes based on OPENCV

Two weeks ago, I received a teacher’s request to participate in a pattern recognition competition. The content is probably to detect the faces that appear in the video, and then count the number. At first, I didn’t have a clue when I saw it. After all, this is my first contact with machine vision. I had no direction. Later, I started to learn OPENCV when I met with my classmates. I have been helping me debug the program. Thank him here. I don’t know if it is an original writing for the first time. If there is any violation, please let me know. I will change and delete it. Thank you.

Start learning

In my sophomore year, I have come into contact with 51 and 32 types of single-chip microcomputers. The most impressive is the STM32-based OPENMV. The ready-made library and machine vision that can be called in time surprised me for the first time, so I went online to find the CV entry. The video probably understands the programming environment, language, etc. Although classmate Amway VSCODE, Pycharm was used in the end, and the language was Python 3.5. Of course, I have benefited a lot from reading a lot of articles on CSDN.

Function code

The blogs of the big guys to learn from:
https://blog.csdn.net/qq_40985985/article/details/108364144
https://blog.csdn.net/sinat_26917383/article/details/70287521
https://blog.csdn. net/haoji007/article/details/106368793/ (Original: https://zhuanlan.zhihu.com/p/80328340)
Due to the special requirements of the game (I feel like this), there is no prior model training for the materials, so it cannot Run neural network and other algorithms, read the blogs of the big guys above, and decided to use OPENCV's built-in cascade + difference hash algorithm to achieve it. Thank you for your contributions

import cv2


# 差异值哈希算法
def dhash(image):
    # 将图片转化为8*8
    image = cv2.resize(image, (9, 8), interpolation=cv2.INTER_CUBIC)
    # 将图片转化为灰度图
    dhash_str = ''
    for i in range(8):
        for j in range(8):
            if image[i, j] > image[i, j + 1]:
                dhash_str = dhash_str + '1'
            else:
                dhash_str = dhash_str + '0'
    result = ''
    for i in range(0, 64, 4):
        result += ''.join('%x' % int(dhash_str[i: i + 4], 2))
    # print("dhash值",result)
    return result

if __name__ == '__main__':

    faceGascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_alt2.xml")
    # 调用级联器
    cap = cv2.VideoCapture("Resources/test1.mp4")
    # 捕获视频帧
    # cap = cv2.imread("Resources/1.png")
    list1 = []
    list2 = []
    flag = 1

    while cap.isOpened():
        success, frame = cap.read()
        if not success:
            break
        ImgGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # 转换为灰度图像,在这里转换了,因此计算哈希值时的图像转换被我删除了
        ImgGray2 = ImgGray[200:1240, 0:2560]
        # 将图像进行了裁剪,因为素材时一段监控录像,怕上面时间日期等影响效果
        ImgGray2 = cv2.resize(ImgGray2, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
        # 缩小了图像的分辨率来加快识别速度
        faces = faceGascade.detectMultiScale(ImgGray2, 1.2, 10)

        for (x, y, w, h) in faces:
            cv2.rectangle(ImgGray2, (x, y), (x+w, y+h), (255, 0, 0), 2)
            image = (ImgGray2[y:y+h, x:x+w])

            hash2 = dhash(image)

            if flag == 1:
                flag = 0
                list1.append(hash2)
            list1.append(hash2)
            n = 0
            j = 0
            for g in range(len(list1)-1):
            # for hash1 in list1:
                for k in range(len(list1[g])):
                    if (list1[g])[k] == hash2[k]:
                        n = n + 1
                m = n
                n = 0
                # print(m)
                if m > 6:
                    j = j+1
                else:
                    pass
            if j == 0:
                list2.append(hash2)
        # 来查重比较新捕获哈希值是否重复
        print(len(list2))
        # 打印出捕获的哈希值数量,即识别到的人数
        cv2.imshow('video', ImgGray2)
        cv2.waitKey(1)
    cap.release()
    cv2.destroyAllWindows()

Insert picture description here

to sum up

Keep capturing the hash value of the face, then delete the duplicate and add new ones, by changing

faces = faceGascade.detectMultiScale(ImgGray2, 1.2, 10)

Two parameters in 1.2 and 10

if m > 6:
                    j = j+1

There is also m , the number of repeated bits of the hash value to adjust the detection result, and finally reach a value that is satisfactory in speed and accuracy.
Since the video used for the exercise is a surveillance video, screenshots or the like will not be released. Finally, I thank the classmates who helped me guide and debug the program, and thank you seniors again.

Guess you like

Origin blog.csdn.net/claymore_2142/article/details/109390903