在视频中取一张人脸存入mysql数据库

import cv2
import os
import pymysql
def main():
    cam = cv2.VideoCapture("1.mp4")
    cam.set(3, 640)
    cam.set(4, 480)
    count = 0
    faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    while (cam.isOpened()):
        ret, frame = cam.read()
        if ret == True:
            cv2.imshow('image', frame)
            count += 1
            if not os.path.isdir("frame"):
                os.mkdir("frame")
            if count==30:
                conn = pymysql.connect(host='localhost',
                                       port=3306,
                                       user="root",
                                       passwd='',
                                       # db="images",
                                       db="mysql",
                                       charset='utf8',
                                       use_unicode=True)
                cursor = conn.cursor()
                x = input("输入英文名:")
                y = input("输入中文名:")
                w = input("输入邮箱:")
                h = input("输入电话号码:")
                parme = [x, y, w, h]
                sql = "INSERT INTO information (yname, name,email,telephone) VALUES (%s,%s,%s,%s)"
                cursor.execute(sql, parme)
                conn.commit()
                cv2.imwrite("./data1/{}.jpg".format(x), frame)
                brr=open("./data1/{}.jpg".format(x),"rb+")
                brr=brr.read()
                sql = "INSERT INTO images (data, name) VALUES (%s, '{0}')".format(x)
                cursor.execute(sql, brr)
                conn.commit()
                cursor.close()
                conn.close()
                # os.remove("./data1/{}.jpg".format(x))
                print('写入 {} 图片成功'.format(x))
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = faceCascade.detectMultiScale(
                gray,
                scaleFactor=1.1,
                minNeighbors=5,
                minSize=(30, 30),
                flags=cv2.CASCADE_SCALE_IMAGE
            )
            for (x, y, w, h) in faces:
                cv2.line(frame, (x, y + int((h) / 4)), (x, y), (0, 0, 255), 2)
                cv2.line(frame, (x, y), (int(x + (w) / 4), y), (0, 0, 255), 2)
                cv2.line(frame, (x + int(3 * (w) / 4), y), (w + x, y), (0, 0, 255), 2)
                cv2.line(frame, (w + x, y), (w + x, y + int((h) / 4)), (0, 0, 255), 2)
                cv2.line(frame, (w + x, int(y + 3 * (h) / 4)), (w + x, h + y), (0, 0, 255), 2)
                cv2.line(frame, (w + x, h + y), (x + int(3 * (w) / 4), h + y), (0, 0, 255), 2)
                cv2.line(frame, (int(x + (w) / 4), h + y), (x, h + y), (0, 0, 255), 2)
                cv2.line(frame, (x, h + y), (x, int(y + 3 * (h) / 4)), (0, 0, 255), 2)
            cv2.imshow('image', frame)
            k = cv2.waitKey(30) & 0xff
            if k == 27:
                break
        else:
             break
    cam.release()
    cv2.destroyAllWindows()
if __name__ == '__main__':
    main()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧鼎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值