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()
在视频中取一张人脸存入mysql数据库
最新推荐文章于 2023-12-15 16:24:15 发布