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

标签 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
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()

变量file_name存储用户ID和当前时间戳,但是当我尝试在cv2.imwrite()中使用它时,它可以写框架的名称,因此它什么也不做。
怎么了

最佳答案

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

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

这解决了问题

关于python - cv2.imwrite()未将时间戳写入帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51490371/

相关文章:

Java OpenCV 将小图像分层到具有透明度的大图像上

python - 如何使用 Python/Opencv 分割二值图像中的附近元素

javascript - 使用 Django-chartit 更改 Highcharts 标签和排序 x 轴

python - 3d numpy 数组的模式/中值/平均值

python - 如何跟踪一段代码中变量的使用?

OpenCV:在顶部和底部配置中绘制匹配项?

c++ - openCV 中带垫子的动画

python - 数学函数python代码检查

python - Pandas 数据框 to_html 单元格对齐

python - 计算图像中特定颜色强度的像素