颜值检测小助手

#颜值检测小助手的接口
import base64
import cv2 as cv
from aip import AipFace

class Image:
    """这个模块主要用于调用摄像头以及生成图片"""
    def __init__(self):
        # self.mv = cv.VideoCapture(0)
        pass

    def video_demo(self):
        """调用摄像头,生成照片"""
        print("正在打开摄像头......")
        mv = cv.VideoCapture(0)  # 打开摄像头
        print("s.开始检测\nq.退出检测")
        while True:  # 循环
            ret, frame = mv.read()  # 读取视频帧
            frame_1 = cv.flip(frame, 1)  # 将视频帧切片
            cv.imshow('frame', frame_1)
            c = cv.waitKey(50)  # 等待
            if c == 115:  # s 键
                # img = cv.resize(frame_1, (71, 99), interpolation=cv.INTER_CUBIC)
                cv.imwrite('1.jpg', frame_1, [int(cv.IMWRITE_PNG_COMPRESSION), 3])
                mv.release()
                cv.destroyAllWindows()
                return "s"
            elif c == 113:
                return "q"

    def token_access(self):
        """进行百度云用户身份验证"""
        APP_ID = "#"
        API_KEY = "#"
        SECRECT_KEY = "#"
        client = AipFace(APP_ID, API_KEY, SECRECT_KEY)
        return client

    def face_detection(self, client, img='1.jpg',  person_num=0):
        """颜值检测"""
        f = open(img, "rb")
        image = base64.b64encode(f.read())
        image = str(image, "utf-8")
        imageType = "BASE64"
        options = {}
        options["face_field"] = "age,faceshape,beauty,gender"
        res = client.detect(image, imageType, options)
        # print(res["result"])
        if res["result"] != None:
            return res
        else:
            print("没有检测到人脸")
            return

    def show_result(self, res):
        """返回检测结果"""
        img = cv.imread('1.jpg')
        print("s.生成结果\nq.重新检测")
        while True:
            sex = res["result"]["face_list"][0]["gender"]['type']
            age = res["result"]["face_list"][0]["age"]
            face_shape = res["result"]["face_list"][0]["face_shape"]['type']
            beauty = res["result"]["face_list"][0]["beauty"]
            beauty = round(beauty, 2)
            # text = "sex:"+sex+"\n" + "age:"+str(age) + "\n" + "face_shape:"+face_shape+"\n"+"beauty:"+str(beauty)
            text1 = "sex:" + sex
            text2 = "age:" + str(age)
            text3 = "face_shape:" + face_shape
            text4 = "beauty:" + str(beauty)
            cv.putText(img, text1, (400, 40), cv.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255), 2)
            cv.putText(img, text2, (400, 80), cv.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255), 2)
            cv.putText(img, text4, (400, 160), cv.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255), 2)
            cv.putText(img, text3, (400, 120), cv.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255), 2)
            # print(img.shape)
            img = cv.rectangle(img, (0, 0), (640, 480), (0, 255, 0), 3)
            cv.imshow("ori_image", img)
            key = cv.waitKey(delay=1)
            if key == 115:
                cv.imwrite("new_img.png", img)
                return 1
            elif key == 113:
                return 0
#调用自己的接口
from Image import *
def main():
    img = Image()
    res = img.video_demo()
    if res == "s":
        client = img.token_access()
        resl = img.face_detection(client)
        c = img.show_result(resl)
        if c == 1:
            return
        elif c == 0:
            main()
    elif res == "q":
        print("没有检测到人脸")

main()

猜你喜欢

转载自blog.csdn.net/Zp18189530679/article/details/84337274