1. 程式人生 > >顏值檢測小助手

顏值檢測小助手

#顏值檢測小助手的介面
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()