Visual studio code doesn't import opencv library into python - python

I was trying to write a python file that opens the camera,only that when I write import cv2 it gives me an error

Try the following:
import cv2
vc = cv2.VideoCapture(0)
while True:
_, frame = vc.read()
cv2.imshow('frame', frame)
vc.release()
cv2.destroyAllWindows()
If this does not work, can you specify your error?

Related

Py to exe error (ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation)

Use open cv for qr reading
if i try to converting exe i face some errors
`def qrread():
import cv2
import os
import ast
from datetime import datetime
# initalize the cam
cap = cv2.VideoCapture(0)
# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()
while True:
_, img = cap.read()
# detect and decode
data, bbox, _ = detector.detectAndDecode(img)
# check if there is a QRCode in the image
if data:
a=data
break
# display the result
cv2.imshow("QRCODEscanner", img)
if cv2.waitKey(1) == ord("q"):
break
`[After the coverting py to exe i face this error (ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.)][1]

Why is Python Open CV imwrite unable to save a simple image from my webcam?

My goal is to capture some images from my laptop's webcam to use them later. The main problem is that cv2.imwrite doesn't seem to work, the directory is created succesfully but I cannot find a way to save the image. I am currently on Manjaro Linux. I've alredy checked that the frame is not empty and in other Python scripts I've been able to show the image properly, the only problem seems to be when I try to save the image.
Is there any other way to save the image or is something wrong with my code?
I have the following Python code:
import cv2 #opencv
import os
import time
import uuid
IMAGES_PATH = 'Tensorflor/workspace/images/collectedimages'
labels = ['hola', 'gracias', 'si', 'no', 'tequiero']
img_number = 15
for label in labels:
!mkdir {'Tensorflow/workspace/images/collectedimages/'+label}
cap = cv2.VideoCapture(0)
print('Collecting images for {}'.format(label))
time.sleep(5)
for numimg in range(img_number):
ret, frame = cap.read()
img_name = os.path.join(IMAGES_PATH, label, label+'.'+'{}.jpg'.format(str(uuid.uuid1())))
cv2.imwrite(img_name,frame)
cv2.imshow('Frame',frame)
time.sleep(2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()

How to save the capture image file in USB using python opencv function named "imwrite" in Linux

I tried to save capture of webcam image in USB using Python on Linux environment.
"Imwrite" is work in file directory but not work in USB directory.
I tried on 'os' package and path.
Is there other method to doing this?
path='/media/odroid/MYUSB/savefolder/'
capture_img=/demo/capture.jpg
image=cv2.imread(capture_img)
cv2.imwrite(os.path.join(path, resave.jpg),image)
The whole code is running without error, but jpg file is not saved in MYUSB
Perhaps you don't need to use os.join() try this:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
savePath = 'output.jpg' #Replace this with your own path say /media/odroid/MYUSB/savefolder/output.jpg
ret, frame = cap.read()
cv2.imwrite(savePath,frame)
If you want to save the whole video please refer to this answer
Here is the code:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
savePath = 'output.avi' #Replace this with your own path say /media/odroid/MYUSB/savefolder/output.avi
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(savePath, fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
while True:
newret, newframe = cap.read()
cv2.imshow('orig',newframe)
out.write(newframe)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
ret, frame = cap.read()
cv2.imwrite(savePath,frame)
cap.release()
out.release()
cv2.destroyAllWindows()
Also, your code has some issues, here is a fixed version of it which should work:
path='/media/odroid/MYUSB/savefolder/'
capture_img='/demo/capture.jpg' #it seems path should be demo/capture.jpg
image=cv2.imread(capture_img)
cv2.imwrite(os.path.join(path, 'resave.jpg'),image)

Opencv Error:(-215)

I am writing this simple code but this is showing error saying size.width>0&&size.height>0 in function imshow()enter code here
import numpy as np
import cv2
img = cv2.imread('C:/Users/Desktop/x.jpg',0)
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
cv2.imshow('image',img)
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
cv2.waitKey(0)
cv2.destroyAllWindows()
Assuming you are using Windows, write path name as:
img = cv2.imread('C:\\Users\\Desktop\\x.jpg', 0)
to load the image correctly.

cv2.VideoCapture freezes on this specific video

For some reason, it hangs on this video:
Here's the code
import cv2
import time
cap = cv2.VideoCapture("http://192.65.213.243/mjpg/video.mjpg")
while(cap.isOpened()):
ret, img = cap.read()
current_time_in_milliseconds = "%.5f" % time.time()
filename="{}.jpg".format(current_time_in_milliseconds)
cv2.imwrite(filename, img)
Any ideas why? Is it something about this video format?
This code works on other mjpg but something about this feed that makes python freeze at cv2.VideoCapture()
I do get this funny error too:
warning: Error opening file
(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:792) warning:
http://192.65.213.243/mjpg/video.mjpg
(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:793)

Categories

Resources