在使用python使用threading、os、cv2进行多线程文件的图像的获取存储、读取、删除的过程中,遇到了一些无法解决的问题:
代码如下:
import cv2
import time
import threading
import os
#从电脑摄像头获取图片并存储
def cunchu():
cap = cv2.VideoCapture(0)
success,x_img=cap.read()
cv2.imwrite(os.path.join('C:\\Users\\10605\\Desktop\\wjj','1.png'), x_img)
#读取图片
def duqv():
img = cv2.imread('C:\\Users\\10605\\Desktop\\wjj\\1.png')
cv2.imshow("img", img)
cv2.waitKey(1)
#删除图片
def shanchu():
os.remove('C:\\Users\\10605\\Desktop\\wjj\\1.png')
if __name__== '__main__':
cunchu1 = threading.Thread(target=cunchu)
duqv1 = threading.Thread(target=duqv)
shanchu1 = threading.Thread(target=shanchu)
cunchu1.start()
time.sleep(1)
print("1")
duqv1.start()
time.sleep(2)
print("2")
shanchu1.start()
time.sleep(1)
print("3")
报错如下
C:\Users\10605\Desktop\venv\Scripts\python.exe C:/Users/10605/Desktop/venv/图片实验.py
1
[ WARN:0@1.422] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('C:\Users\10605\Desktop\wjj\1.png'): can't open/read file: check file path/integrity
Exception in thread Thread-2 (duqv):
Traceback (most recent call last):
File "C:\Users\10605\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\Users\10605\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\10605\Desktop\venv\图片实验.py", line 15, in duqv
cv2.imshow("img", img)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
2
3
进程已结束,退出代码0