我将从网络摄像头获得的帧值附加到名为final_frame的列表中。然后,我将其写入文件并将其转换为str。在另一个代码中,我正在打开文件并读取值,然后尝试从中制作视频。当我在记事本或代码中打开文件时,在两个文件之间看到“ .....”。麻木的。这不允许我用它制作视频。请查看下面的代码和图像:capturevideo.py:
import cv2 , time , numpy
video= cv2.VideoCapture(0 , cv2.CAP_DSHOW)
a=1
final_frame =[]
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# out = cv2.VideoWriter('output.mp4v' , fourcc , 20.0 , (640 , 480))
while True:
a = a+1
check, frame = video.read()
# print(check)
grey = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY)
final_frame.append(grey)
# out.write(frame)
# time.sleep(3)
cv2.imshow("capturing" , grey)
key = cv2.waitKey(1)
if key==ord('q'):
break
print(a)
video.release()
cv2.destroyAllWindows()
print(type(final_frame))
with open ("numpyarrays.txt" , 'w') as file:
file.write(str(final_frame))
创建的文本文件,中间有许多点。即使在记事本或vscode上将其打开,也会出现该消息。
[array([[ 90, 89, 80, ..., 172, 173, 175],
[ 89, 88, 83, ..., 172, 172, 171],
[ 89, 90, 83, ..., 169, 169, 167],
...,
[ 47, 52, 53, ..., 124, 126, 128],
[ 50, 55, 55, ..., 125, 129, 131],
[ 52, 54, 53, ..., 128, 128, 128]], dtype=uint8), array([[100, 100, 101, ..., 169, 172, 168],
[100, 100, 100, ..., 169, 172, 165],
[103, 100, 95, ..., 173, 176, 173],
...,
[ 44, 43, 47, ..., 115, 116, 125],
[ 47, 49, 53, ..., 116, 121, 131],
[ 53, 54, 58, ..., 121, 123, 126]], dtype=uint8), array([[ 87, 91, 91, ..., 169, 168, 167],
[ 93, 95, 95, ..., 169, 169, 168],
[100, 100, 98, ..., 173, 171, 167],
...,
[ 51, 52, 52, ..., 121, 121, 124],
[ 58, 58, 57, ..., 129, 128, 129],
[ 62, 61, 58, ..., 132, 129, 129]], dtype=uint8), array([[ 91, 93, 97, ..., 173, 170, 166],
[ 92, 95, 98, ..., 173, 173, 167],
[ 93, 96, 103, ..., 175, 172, 162],
...,
[ 55, 55, 57, ..., 129, 131, 133],
[ 59, 61, 62, ..., 129, 131, 131],
[ 61, 62, 62, ..., 128, 130, 130]], dtype=uint8), array([[101, 101, 101, ..., 167, 167, 168],
[101, 102, 100, ..., 167, 167, 168],
[ 98, 100, 95, ..., 168, 168, 171],
...,
[ 55, 61, 58, ..., 128, 128, 128],
[ 53, 54, 54, ..., 128, 128, 126],
[ 52, 53, 51, ..., 128, 128, 126]], dtype=uint8), array([[ 86, 90, 95, ..., 169, 169, 172],
[ 84, 87, 102, ..., 167, 165, 173],
[ 86, 87, 100, ..., 166, 168, 173],
...,
[ 55, 52, 50, ..., 120, 124, 129],
[ 55, 51, 50, ..., 121, 125, 128],
[ 51, 50, 50, ..., 124, 128, 126]], dtype=uint8), array([[ 96, 101, 102, ..., 173, 174, 172],
[ 98, 102, 103, ..., 170, 172, 171],
[ 96, 98, 102, ..., 163, 165, 164],
...,
[ 52, 54, 61, ..., 124, 119, 128],
[ 50, 57, 64, ..., 131, 124, 124],
[ 50, 55, 62, ..., 133, 126, 123]], dtype=uint8), array([[102, 99, 95, ..., 173, 172, 169],
[ 99, 98, 95, ..., 173, 175, 169],
[ 98, 97, 96, ..., 168, 166, 168],
...,
[ 50, 53, 54, ..., 125, 129, 131],
[ 53, 59, 58, ..., 125, 129, 131],
[ 58, 65, 65, ..., 123, 128, 130]], dtype=uint8), array([[101, 102, 102, ..., 171, 171, 172],
[100, 101, 101, ..., 170, 170, 174],
[ 96, 97, 96, ..., 173, 169, 175],
...,
[ 44, 45, 50, ..., 126, 129, 129],
[ 47, 45, 46, ..., 128, 130, 130],
[ 48, 45, 44, ..., 126, 129, 130]], dtype=uint8), array([[101, 102, 102, ..., 167, 167, 166],
[101, 109, 118, ..., 166, 171, 164],
[100, 119, 118, ..., 164, 171, 172],
...,
[ 50, 51, 51, ..., 127, 129, 130],
[ 53, 52, 52, ..., 128, 128, 129],
[ 52, 52, 53, ..., 129, 128, 130]], dtype=uint8), array([[107, 104, 102, ..., 168, 169, 169],
[105, 104, 101, ..., 169, 169, 167],
[105, 99, 100, ..., 166, 161, 159],
...,
[ 55, 57, 58, ..., 135, 129, 126],
[ 58, 58, 59, ..., 125, 126, 124],
[ 59, 60, 64, ..., 123, 123, 123]], dtype=uint8), array([[ 95, 95, 96, ..., 171, 172, 174],
[ 98, 100, 95, ..., 169, 165, 178],
[100, 98, 96, ..., 166, 169, 178],
...,
[ 51, 57, 61, ..., 124, 126, 129],
[ 50, 54, 56, ..., 124, 125, 125],
[ 50, 52, 51, ..., 123, 124, 123]], dtype=uint8), array([[ 96, 91, 93, ..., 165, 170, 167],
[ 96, 95, 99, ..., 164, 165, 168],
[102, 100, 101, ..., 164, 165, 161],
...,
[ 52, 54, 53, ..., 125, 124, 126],
[ 55, 58, 59, ..., 125, 125, 131],
[ 58, 59, 60, ..., 126, 129, 133]], dtype=uint8), array([[103, 104, 103, ..., 165, 164, 165],
[103, 102, 102, ..., 169, 164, 168],
[ 98, 97, 97, ..., 171, 171, 171],
...,
[ 58, 61, 55, ..., 125, 126, 123],
[ 57, 57, 55, ..., 125, 125, 124],
[ 47, 47, 54, ..., 126, 126, 128]], dtype=uint8), array([[ 99, 93, 91, ..., 174, 172, 169],
[101, 95, 92, ..., 175, 171, 168],
[100, 100, 94, ..., 168, 168, 167],
...,
[ 51, 53, 54, ..., 126, 131, 132],
[ 54, 57, 61, ..., 129, 129, 130],
[ 61, 64, 68, ..., 130, 130, 129]], dtype=uint8), array([[ 96, 96, 100, ..., 171, 172, 172],
[ 96, 97, 100, ..., 169, 173, 169],
[ 98, 100, 108, ..., 162, 165, 168],
...,
[ 46, 52, 52, ..., 132, 132, 132],
[ 58, 59, 55, ..., 133, 132, 135],
[ 58, 60, 58, ..., 135, 133, 135]], dtype=uint8), array([[ 95, 98, 102, ..., 167, 165, 166],
[ 95, 97, 101, ..., 165, 157, 166],
[ 95, 96, 98, ..., 162, 162, 167],
...,
[ 52, 54, 55, ..., 132, 133, 130],
[ 50, 51, 51, ..., 133, 132, 134],
[ 50, 53, 51, ..., 128, 129, 125]], dtype=uint8), array([[ 88, 95, 94, ..., 172, 167, 167],
[ 88, 94, 95, ..., 165, 166, 167],
[ 89, 91, 91, ..., 166, 167, 168],
...,
[ 49, 51, 53, ..., 125, 125, 124],
[ 48, 52, 55, ..., 125, 128, 128],
[ 50, 53, 57, ..., 126, 129, 130]], dtype=uint8), array([[ 88, 89, 89, ..., 173, 173, 174],
[ 86, 86, 96, ..., 173, 175, 168],
[ 93, 91, 93, ..., 172, 175, 167],
...,
[ 55, 55, 53, ..., 125, 125, 126],
[ 61, 57, 53, ..., 125, 129, 132],
[ 62, 57, 53, ..., 128, 129, 129]], dtype=uint8), array([[100, 96, 96, ..., 172, 168, 164],
[ 98, 99, 105, ..., 173, 168, 168],
[ 97, 101, 108, ..., 168, 168, 171],
...,
[ 44, 44, 47, ..., 123, 123, 125],
[ 47, 48, 50, ..., 129, 125, 126],
[ 54, 59, 55, ..., 127, 127, 127]], dtype=uint8), array([[ 88, 83, 82, ..., 171, 170, 168],
[ 87, 84, 81, ..., 174, 169, 167],
[ 90, 89, 87, ..., 167, 169, 168],
...,
[ 74, 49, 52, ..., 125, 126, 128],
[ 68, 34, 38, ..., 126, 128, 130],
[ 67, 58, 48, ..., 126, 128, 130]], dtype=uint8), array([[ 97, 96, 96, ..., 169, 168, 171],
[ 96, 95, 95, ..., 168, 169, 173],
[ 93, 91, 95, ..., 169, 171, 169],
...,
[ 50, 47, 47, ..., 129, 129, 122],
[ 50, 47, 47, ..., 131, 130, 121],
[ 55, 52, 51, ..., 131, 131, 125]], dtype=uint8), array([[ 95, 93, 91, ..., 167, 168, 168],
[ 96, 94, 90, ..., 166, 167, 171],
[100, 96, 90, ..., 165, 172, 171],
...,
[ 57, 57, 57, ..., 123, 124, 123],
[ 61, 58, 55, ..., 124, 124, 121],
[ 61, 58, 53, ..., 125, 125, 118]], dtype=uint8), array([[ 96, 96, 98, ..., 172, 173, 172],
[ 97, 98, 91, ..., 172, 172, 171],
[ 97, 110, 102, ..., 172, 173, 171],
...,
[ 48, 55, 59, ..., 123, 124, 132],
[ 53, 59, 61, ..., 124, 124, 130],
[ 54, 60, 61, ..., 124, 123, 128]], dtype=uint8), array([[ 93, 101, 107, ..., 170, 169, 170],
[ 93, 100, 103, ..., 170, 174, 167],
[ 94, 96, 100, ..., 171, 172, 167],
...,
[ 53, 54, 58, ..., 125, 125, 128],
[ 57, 58, 58, ..., 126, 128, 130],
[ 58, 58, 57, ..., 121, 126, 131]], dtype=uint8), array([[ 88, 86, 91, ..., 168, 166, 168],
[ 89, 87, 93, ..., 171, 169, 165],
[ 90, 89, 94, ..., 175, 171, 166],
...,
[ 54, 57, 62, ..., 130, 129, 129],
[ 54, 61, 65, ..., 130, 130, 129],
[ 59, 61, 62, ..., 125, 128, 125]], dtype=uint8), array([[ 97, 98, 104, ..., 174, 172, 167],
[ 96, 98, 101, ..., 174, 173, 168],
[ 93, 96, 96, ..., 173, 175, 173],
...,
[ 54, 38, 58, ..., 128, 132, 128],
[ 53, 51, 59, ..., 127, 129, 127],
[ 59, 57, 58, ..., 127, 126, 125]], dtype=uint8), array([[ 95, 101, 103, ..., 173, 176, 175],
[ 93, 95, 100, ..., 172, 176, 176],
[ 89, 87, 93, ..., 173, 177, 175],
...,
[ 55, 57, 59, ..., 125, 125, 127],
[ 60, 61, 61, ..., 126, 128, 130],
[ 64, 62, 60, ..., 128, 128, 129]], dtype=uint8), array([[ 95, 96, 90, ..., 170, 167, 165],
[ 96, 97, 93, ..., 169, 166, 165],
[ 97, 98, 97, ..., 169, 165, 167],
...,
[ 52, 51, 52, ..., 121, 122, 116],
[ 51, 50, 51, ..., 121, 124, 121],
[ 54, 47, 47, ..., 117, 124, 124]], dtype=uint8), array([[ 87, 89, 92, ..., 177, 176, 177],
[ 89, 96, 95, ..., 176, 176, 180],
[ 88, 93, 94, ..., 177, 176, 177],
...,
[ 55, 58, 57, ..., 132, 131, 125],
[ 58, 58, 58, ..., 128, 130, 125],
[ 58, 59, 58, ..., 129, 128, 126]], dtype=uint8), array([[ 94, 96, 101, ..., 169, 170, 169],
[ 95, 97, 100, ..., 169, 169, 166],
[ 96, 98, 100, ..., 173, 167, 165],
...,
[ 43, 45, 54, ..., 135, 131, 131],
[ 48, 51, 56, ..., 130, 128, 129],
[ 53, 54, 55, ..., 131, 129, 129]], dtype=uint8), array([[101, 98, 98, ..., 169, 165, 166],
[100, 96, 97, ..., 171, 173, 167],
[ 95, 95, 96, ..., 169, 174, 170],
...,
[ 54, 55, 59, ..., 129, 125, 129],
[ 55, 57, 59, ..., 124, 124, 129],
[ 55, 58, 58, ..., 130, 130, 130]], dtype=uint8), array([[ 89, 91, 94, ..., 169, 167, 167],
[ 87, 89, 91, ..., 169, 169, 167],
[ 86, 83, 91, ..., 172, 171, 169],
...,
[ 51, 53, 54, ..., 132, 125, 125],
[ 59, 58, 54, ..., 138, 131, 130],
[ 64, 62, 55, ..., 121, 125, 128]], dtype=uint8), array([[103, 102, 99, ..., 170, 172, 172],
[102, 109, 111, ..., 171, 172, 172],
[102, 117, 115, ..., 171, 167, 169],
...,
[ 46, 57, 62, ..., 125, 128, 126],
[ 47, 36, 60, ..., 124, 125, 126],
[ 53, 53, 61, ..., 123, 123, 124]], dtype=uint8), array([[104, 98, 97, ..., 167, 169, 167],
[104, 98, 96, ..., 167, 169, 165],
[103, 99, 97, ..., 168, 169, 170],
...,
[ 44, 44, 44, ..., 128, 134, 132],
[ 46, 46, 45, ..., 135, 140, 137],
[ 50, 50, 46, ..., 130, 136, 131]], dtype=uint8), array([[ 88, 89, 93, ..., 169, 168, 166],
[ 93, 93, 93, ..., 168, 167, 166],
[ 95, 97, 95, ..., 171, 171, 171],
...,
[ 53, 52, 53, ..., 126, 131, 136],
[ 66, 62, 58, ..., 124, 129, 133],
[ 69, 62, 59, ..., 122, 125, 130]], dtype=uint8), array([[ 95, 103, 102, ..., 169, 173, 174],
[ 98, 103, 102, ..., 164, 172, 169],
[ 98, 101, 101, ..., 166, 171, 169],
...,
[ 50, 33, 59, ..., 118, 122, 121],
[ 55, 58, 67, ..., 119, 118, 124],
[ 55, 59, 66, ..., 124, 128, 129]], dtype=uint8), array([[102, 102, 104, ..., 168, 169, 167],
[103, 102, 109, ..., 171, 171, 172],
[102, 100, 100, ..., 168, 169, 168],
...,
[ 54, 58, 54, ..., 126, 136, 132],
[ 60, 61, 58, ..., 131, 139, 138],
[ 62, 62, 59, ..., 132, 131, 132]], dtype=uint8), array([[ 90, 94, 95, ..., 166, 169, 172],
[ 93, 94, 95, ..., 165, 170, 172],
[102, 98, 95, ..., 166, 160, 164],
...,
[ 50, 48, 52, ..., 135, 128, 128],
[ 55, 51, 55, ..., 139, 131, 129],
[ 57, 55, 62, ..., 129, 128, 126]], dtype=uint8), array([[ 95, 94, 93, ..., 167, 165, 168],
[ 94, 95, 95, ..., 168, 167, 168],
[ 93, 96, 95, ..., 165, 167, 168],
...,
[ 60, 62, 60, ..., 125, 128, 129],
[ 61, 62, 61, ..., 125, 128, 128],
[ 62, 61, 61, ..., 125, 125, 126]], dtype=uint8), array([[104, 100, 95, ..., 177, 176, 175],
[104, 98, 95, ..., 176, 176, 172],
[103, 100, 99, ..., 171, 168, 169],
...,
[ 60, 61, 88, ..., 128, 129, 129],
[ 60, 59, 75, ..., 130, 130, 129],
[ 60, 54, 54, ..., 128, 128, 128]], dtype=uint8), array([[ 96, 93, 90, ..., 172, 174, 169],
[ 98, 97, 93, ..., 171, 169, 167],
[ 97, 97, 97, ..., 167, 166, 166],
...,
[ 43, 51, 57, ..., 128, 125, 122],
[ 48, 51, 55, ..., 121, 123, 124],
[ 59, 54, 55, ..., 122, 124, 126]], dtype=uint8), array([[ 99, 95, 94, ..., 169, 167, 166],
[ 97, 95, 92, ..., 168, 166, 167],
[ 93, 93, 88, ..., 168, 166, 167],
...,
[ 48, 50, 58, ..., 124, 135, 133],
[ 52, 53, 58, ..., 119, 133, 135],
[ 51, 56, 60, ..., 135, 138, 139]], dtype=uint8), array([[102, 101, 98, ..., 168, 171, 171],
[101, 102, 102, ..., 168, 166, 174],
[101, 107, 104, ..., 168, 167, 172],
...,
[ 54, 55, 58, ..., 129, 125, 124],
[ 55, 58, 59, ..., 137, 130, 128],
[ 55, 59, 60, ..., 132, 131, 130]], dtype=uint8), array([[100, 102, 100, ..., 171, 169, 169],
[101, 100, 97, ..., 172, 171, 171],
[101, 98, 96, ..., 172, 171, 172],
...,
[ 53, 52, 52, ..., 131, 131, 131],
[ 54, 53, 51, ..., 126, 128, 130],
[ 57, 55, 52, ..., 124, 126, 128]], dtype=uint8), array([[ 88, 90, 90, ..., 173, 173, 174],
[ 95, 95, 91, ..., 173, 173, 169],
[105, 97, 90, ..., 178, 171, 171],
...,
[ 45, 45, 46, ..., 121, 125, 124],
[ 46, 47, 47, ..., 130, 130, 126],
[ 44, 48, 51, ..., 129, 131, 128]], dtype=uint8)]
因此,当我运行另一个代码(torun.py)时,出现此错误:
代码-torun.py
import cv2, numpy, time
with open ("numpyarrays.txt" , 'r') as file:
cont = file.read()
print(type(cont))
frame = numpy.asarray(cont)
print(type(frame))
frame = frame.astype(numpy.uint8)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4v' , fourcc , 20.0 , (640 , 480))
out.write(frame)
out.release()
错误:
Traceback (most recent call last):
File "torun.py", line 7, in <module>
frame = frame.astype(numpy.uint8)
ValueError: invalid literal for int() with base 10: '[array([[ 90, 89, 80, ..., 172, 173, 175],\n
[ 89, 88, 83, ..., 172, 172, 171],\n [ 89, 90, 83, ..., 169, 169, 167],\n ...,\n [ 47, 52, 53, ..., 124, 126, 128],\n
如果删除行-frame = frame.astype(numpy.uint8),则会出现此错误:
Traceback (most recent call last):
File "torun.py", line 11, in <module>
out.write(frame)
TypeError: Expected Ptr<cv::UMat> for argument 'image'
请帮我解决这个问题。根本原因是文件中出现的点。
这是由于str(array)
返回带有点的字符串以提高可读性的事实。执行此操作的最有效方法是使用np.load
和np.save
函数,这些函数比将其存储在文本文件中的速度更快。因此,您要替换的是:
with open ("numpyarrays.txt" , 'w') as file:
file.write(str(final_frame))
作者:
np.save("numpyarrays.npy", final_frame)
并替换:
with open ("numpyarrays.txt" , 'r') as file:
cont = file.read()
print(type(cont))
frame = numpy.asarray(cont)
print(type(frame))
frame = frame.astype(numpy.uint8)
作者:
frame = np.load("numpyarrays.npy")
file.write(str(final_frame))
您无法只需使用str
将任何给定的东西转换为可写到文件的内容。事物本身和事物的表示形式之间存在差异。
final_frame
是一个本地Python列表,其中包含Numpy数组。 Numpy提供a few different函数以将其数据保存到文件中,从而保留所有信息并可以在以后读取。 (您需要阅读文档并选择最适合您的文档。)但是,由于拥有这些数组的列表而使操作变得很复杂。通过堆叠这些数组,使它们具有一个额外的维度会更好。
@@ g2i,保存部分对我来说效果很好,但是当我在另一个脚本中加载它时,在两种情况下都出现错误。保存代码:
import cv2 , time , numpy
video= cv2.VideoCapture(0 , cv2.CAP_DSHOW)
a=1
final_frame =[]
while True:
a = a+1
check, frame = video.read()
grey = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY)
final_frame.append(grey)
cv2.imshow("capturing" , grey)
key = cv2.waitKey(1)
if key==ord('q'):
break
print(a)
video.release()
cv2.destroyAllWindows()
print(type(final_frame))
numpy.save("numpyarrays.npy" , final_frame)
我只是将文件名作为参数提供,因为它位于同一目录中。导入cv2,numpy,时间
# cont = file.read()
# print(type(cont))
# frame = numpy.asarray(cont)
# print(type(frame))
frame = numpy.load(“ numpyarrays.npy”)fourcc = cv2.VideoWriter_fourcc(*'mp4v')out = cv2.VideoWriter('output.mp4v',fourcc,20.0,(640,480))out.write(框架)out.release()
在此,我得到这个错误追溯(最近一次通话):文件“ c:/ Users / Dhruva Jindal / Desktop / STUDIES / Computer / Python / cv2 / torun.py”,第8行框架= numpy.load(“ numpyarrays.npy”)载入中的文件“ C:\ Users \ Dhruva Jindal \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ site-packages \ numpy \ lib \ npyio.py”,第428行fid = open(os_fspath(file),“ rb”)FileNotFoundError:[错误2]没有这样的文件或目录:'numpyarrays.npy'
如果我将整个路径添加到它-
import cv2, numpy, time
# with open ("numpyarrays.txt" , 'r') as file:
# cont = file.read()
# print(type(cont))
# frame = numpy.asarray(cont)
# print(type(frame))
# frame = frame.astype(numpy.uint8)
frame = numpy.load("C:\Users\Dhruva Jindal\Desktop\STUDIES\Computer\Python\cv2\numpyarrays.npy" )
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4v' , fourcc , 20.0 , (640 , 480))
out.write(frame)
out.release()
文件“ c:/ Users / Dhruva Jindal / Desktop / STUDIES / Computer / Python / cv2 / torun.py”,第8行框架= numpy.load(“ C:\ Users \ Dhruva Jindal \ Desktop \ STUDIES \ Computer \ Python \ cv2 \ numpyarrays.npy”)^SyntaxError:(unicode错误)“ unicodeescape”编解码器无法解码位置2-3中的字节:截断的\ UXXXXXXXX转义
尽管事实上这两个文件都在同一目录中:imagw which shows they are in the same directory这是保存在numpyarrays.npy文件中的数据,首先显示-该文件未在编辑器中显示,因为它使用二进制文件或使用不受支持的文本编码。您是否仍要打开它?我单击它,并打开了一个包含一些字符的文件。the numpyarrays,nyp file when i clicked on do you want to open it anyway