Python cv2.imwrite()未将时间戳写入帧

Python cv2.imwrite()未将时间戳写入帧,python,opencv,Python,Opencv,我有一个成功运行的图像识别脚本。我想要的是,在成功识别图像时,它会将捕获的帧和当前时间戳保存到文件名中。 这是我的密码 #!C:\Users\Gurminders\AppData\Local\Programs\Python\Python35-32\python.exe print("Content-type: text/html\n\n"); # Import OpenCV2 for image processing import cv2 import mysql.connector impor

我有一个成功运行的图像识别脚本。我想要的是,在成功识别图像时,它会将捕获的帧和当前时间戳保存到文件名中。 这是我的密码

#!C:\Users\Gurminders\AppData\Local\Programs\Python\Python35-32\python.exe
print("Content-type: text/html\n\n");
# Import OpenCV2 for image processing
import cv2
import mysql.connector
import numpy as np
import os
import sys
import datetime
import time
import cgitb
cgitb.enable()
# put the database inputs here
cnx = mysql.connector.connect(user='root', password='', host='localhost', 
database='image')
cursor = cnx.cursor()
def assure_path_exists(path):
dir = os.path.dirname(path)
if not os.path.exists(dir):
    os.makedirs(dir)

# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.LBPHFaceRecognizer_create()

assure_path_exists("trainer/")

# Load the trained mode
recognizer.read('trainer/trainer.yml')

# Load prebuilt model for Frontal Face
cascadePath = "haarcascade_frontalface_default.xml"
# Create classifier from prebuilt model
faceCascade = cv2.CascadeClassifier(cascadePath);

# Set the font style
font = cv2.FONT_HERSHEY_SIMPLEX
# Initialize and start the video frame capture
cam = cv2.VideoCapture(0)
check = "1" #sys.argv[1]
int_check = int(check)
print(int_check + 5)
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%Y %H:%M:%S')
# Loop
while True:
# Read the video frame
ret, im =cam.read()

# Convert the captured frame into grayscale
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

# Get all face from the video frame
faces = faceCascade.detectMultiScale(gray, 1.2,5)

# For each face in faces
for(x,y,w,h) in faces:

    # Create rectangle around the face
    cv2.rectangle(im, (x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)

    # Recognize the face belongs to which ID
    Id, confidence = recognizer.predict(gray[y:y+h,x:x+w])


    # Check the ID if exist 
    if(Id == int_check):
        confi = round(100 - confidence,2)

        if(confi >20):

            # create table face with two variables id and verified
            cursor.execute ("UPDATE face SET verified=%s WHERE id='%s' " % (confi, Id))


        elif(confi <20):
            cursor.execute ("UPDATE face SET verified='false' WHERE id='%s' " % (Id))






    # Put text describe who is in the picture
    cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
    cv2.putText(im, str(Id), (x,y-40), font, 1, (255,255,255), 3)

# Display the video frame with the bounded rectangle
cv2.imshow('Live Camera Capturing',im)
file_name = str(Id) + " " + str(st)
print(file_name)
cv2.imwrite("face -" + str(file_name) + " frame.jpg", im)

# wait time in ms for camera quit
if cv2.waitKey(1000):
    break


cnx.close()

# Stop the camera
cam.release()

# Close all windows
cv2.destroyAllWindows()
#!C:\Users\Gurminders\AppData\Local\Programs\Python\Python35-32\Python.exe
打印(“内容类型:text/html\n\n”);
#导入OpenCV2进行图像处理
进口cv2
导入mysql.connector
将numpy作为np导入
导入操作系统
导入系统
导入日期时间
导入时间
进口cgitb
cgib.enable()
#将数据库输入放在这里
cnx=mysql.connector.connect(用户='root',密码='',主机='localhost',
数据库(图像)
cursor=cnx.cursor()
def确保路径存在(路径):
dir=os.path.dirname(路径)
如果不存在os.path.exists(目录):
os.makedirs(dir)
#创建用于人脸识别的局部二元模式直方图
recognizer=cv2.face.LBPHFaceRecognizer_create()
确保路径存在(“培训师/”)
#加载训练模式
识别器.read('trainer/trainer.yml'))
#正面载荷预建模型
cascadePath=“haarcascade\u frontalface\u default.xml”
#从预先构建的模型创建分类器
faceCascade=cv2.级联分类器(级联路径);
#设置字体样式
font=cv2.font\u HERSHEY\u SIMPLEX
#初始化并启动视频帧捕获
cam=cv2.视频捕获(0)
check=“1”#sys.argv[1]
int_check=int(check)
打印(内部检查+5)
ts=时间。时间()
st=datetime.datetime.fromtimestamp(ts).strftime(“%d-%m-%Y%H:%m:%S”)
#环路
尽管如此:
#阅读视频帧
ret,im=cam.read()
#将捕获的帧转换为灰度
灰色=cv2.CVT颜色(im、cv2.COLOR\U BGR2GRAY)
#从视频帧中获取所有人脸
面=面级联。检测多尺度(灰色,1.2,5)
#对于面中的每个面
对于面中的(x,y,w,h):
#围绕面创建矩形
cv2.矩形(im,(x-20,y-20),(x+w+20,y+h+20),(0255,0),4)
#识别属于哪个ID的脸
Id,置信度=识别器。预测(灰色[y:y+h,x:x+w])
#如果存在,请检查ID
如果(Id==整数检查):
confi=四舍五入(100-置信度,2)
如果(确认>20):
#创建具有两个变量id和验证的表面
cursor.execute(“已验证的更新面集=%s,其中id='%s'”(确认,id))

elif(confi解决方案是从时间戳中删除冒号

st = datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%Y_%H-%M-%S')

这解决了问题

解决方案是从时间戳中删除冒号

st = datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%Y_%H-%M-%S')

这解决了当我使用print(file_name)时的问题,print(file_name)是时间戳和Id的concat字符串,它打印时没有任何错误。但是对于下一行,它没有给出任何错误,只是不生成带有file_name的帧jpg,如果您使用cc2.putText任何常量固定测试字符串,如“hello world”,有效吗?hello@Micka解决方案是从时间戳中删除冒号:符号,因为文件名中不允许使用冒号。谢谢使用print(文件名)这是时间戳和Id的concat字符串,它打印时没有任何错误。但对于下一行,它不会给出任何错误,只是不生成带有文件名的帧jpg,如果您使用cc2.putText任何常量固定测试字符串,如“hello world”,有效吗?hello@Mika解决方案是从时间戳中删除冒号:符号,因为文件名中不允许使用冒号。谢谢