我尝试运行下面的代码,它运行成功但图像没有保存在系统的任何地方。有谁知道问题出在哪里或解决方案?
import cv2
import time
# open the default camera
cap = cv2.VideoCapture(0)
# set the width and height of the frame
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# set the start time
start_time = time.time()
# loop through the frames
print("looping through frames")
while True:
# capture a frame
ret, frame = cap.read()
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
# define the output file name and format
output_file = f'/Frame_{current_time}.jpg'
output_format = 'JPEG'
# check if the frame is captured successfully
if not ret:
break
# calculate the elapsed time
elapsed_time = time.time() - start_time
# check if 1 second has passed
if elapsed_time >= 1:
print("1 second passed")
# save the frame as an image file
cv2.imwrite(f'/TestFrames/{output_file}', frame)
print(output_file)
print("image saved")
# reset the start time
start_time = time.time()
print("time reset")
# display the frame
cv2.imshow('frame', frame)
# check for the 'q' key to quit
if cv2.waitKey(1) == ord('q'):
break
# release the camera and close all windows
cap.release()
cv2.destroyAllWindows()
我尝试运行下面的代码,它运行成功但图像没有保存在系统的任何地方。
主要问题是图像文件名包含冒号(
:
)字符,冒号字符是无效字符(至少对于Windows)文件名。
为了测试,我们可以检查
cv2.imwrite
的返回状态(状态是True
成功,False
失败):
is_ok = cv2.imwrite(f'/TestFrames/{output_file}', frame)
print(output_file)
if is_ok:
print("image saved")
else:
print("Failed saving the image")
假设我们使用的是 Windows:
我们可以将
:
字符替换为_
例如:
output_file = f'Frame_{current_time}.jpg'
output_file = output_file.replace(":", "_")
下面的回答建议把冒号替换成一个看起来像冒号的unicode字符“꞉”,但是
cv2.imwrite
不支持unicode字符(至少在Windows中不支持)。
如果我们想使用 '
\ua789
' unicode 字符,我们可以使用 cv2.imencode
和二进制文件编写:
with open(f'/TestFrames/{output_file}', 'wb') as f: # Open image file as binary file for writing
cv2.imencode('.'+output_format, frame)[1].tofile(f) # encode the image as JPEG, and write the encoded image to the file (use [1] because cv2.imencode returns a tuple).
使用 unicode 字符代替冒号的代码示例:
import cv2
import time
# open the default camera
cap = cv2.VideoCapture(0)
# set the width and height of the frame
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# set the start time
start_time = time.time()
# loop through the frames
print("looping through frames")
while True:
# capture a frame
ret, frame = cap.read()
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
# define the output file name and format
output_file = f'Frame_{current_time}.jpg'
output_file = output_file.replace(":", "\ua789") # Replace colon character with a character that looks like colon
#output_file = output_file.replace(":", "_") # Replace colon character with a underscore character
output_format = 'JPEG'
# check if the frame is captured successfully
if not ret:
break
# calculate the elapsed time
elapsed_time = time.time() - start_time
# check if 1 second has passed
if elapsed_time >= 1:
print("1 second passed")
# save the frame as an image file
#is_ok = cv2.imwrite(f'/TestFrames/{output_file}', frame)
with open(f'/TestFrames/{output_file}', 'wb') as f: # Open image file as binary file for writing
cv2.imencode('.'+output_format, frame)[1].tofile(f) # encode the image as JPEG, and write the encoded image to the file (use [1] because cv2.imencode returns a tuple).
print(output_file)
print("image saved")
# reset the start time
start_time = time.time()
print("time reset")
# display the frame
cv2.imshow('frame', frame)
# check for the 'q' key to quit
if cv2.waitKey(1) == ord('q'):
break
# release the camera and close all windows
cap.release()
cv2.destroyAllWindows()