I have this script to capture images from webcam, but it is not saving any image, I donot know why. I am getting this error "Traceback (most recent call last): File "C:/Users/Iram/.PyCharmCE2019.3/config/scratches/scratch_5.py", line 26, in gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' [ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback"
My code is
import cv2
import datetime
i = 1
key = cv2.waitKey(1)
webcam = cv2.VideoCapture(0)
while True:
try:
check, frame = webcam.read()
print(check) # prints true as long as the webcam is running
print(frame) # prints matrix values of each framecd
cv2.imshow("Capturing", frame)
key = cv2.waitKey(1)
if key == ord('s'):
cv2.imwrite(filename='saved_img.jpg,%Y-%b-%d %H:%M:%S', img=frame)
#print('Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))
i += 1
print('%i')
# webcam.release()
img_new = cv2.imread('saved_img.jpg,%Y-%b-%d %H:%M:%S', cv2.IMREAD_GRAYSCALE)
# cv2.imshow("Captured Image", img_new)
# cv2.waitKey(1925)
print("Processing image...")
img_ = cv2.imread('saved_img.jpg,%Y-%b-%d %H:%M:%S', cv2.IMREAD_ANYCOLOR)
print("Converting RGB image to grayscale...")
gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
print("Converted RGB image to grayscale...")
print("Resizing image to 28x28 scale...")
img_ = cv2.resize(gray, (28, 28))
print("Resized...")
img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
print("Image saved!")
elif key == ord('q'):
print("Turning off camera.")
webcam.release()
print("Camera off.")
print("Program ended.")
cv2.destroyAllWindows()
break
except(KeyboardInterrupt):
print("Turning off camera.")
webcam.release()
print("Camera off.")
print("Program ended.")
cv2.destroyAllWindows()
camera.release()
break
camera.release()
cv2.destroyAllWindows()
Can anybody help me in this?
break
in theexcept
? – AMC