How to create executable application to using opencv? - python

I am greener in Python .I have design a small application for my personal project . But the problem is that when i use it on python then it's ok after create a executable(exe) application by pyinstaller then it's show "Fatal error" and "cam returned -1" my python project name is cam.py.
enter image description here
I don't know what's the problem of this application .
and my code is below:
import cv2
import numpy
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
while True :
ret,frame = cam.read()
gray = cv2.cvtColor (frame, cv2.COLOR_BGR2BGRA)
faces = face_cascade.detectMultiScale(gray, 1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow("LubnaX(NSL2)",frame)
k = cv2.waitKey(10)
if k==27:
break
cam.release()
cv2.destroyAllWindows()

Related

Error in opening video using openCv (python)?

i'm trying to read videos with OpenCv python, but for some videos i can't do so, and i don't now why, or how to fix it (knowing that i can see the video on my pc )
here's the code i'm using :
# importing libraries
import cv2
video_path = 'C:/Videos/abc.avi'
cap = cv2.VideoCapture(video_path)
if (cap.isOpened() == False):
print("Error opening video")
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
How can i fix this, and make it work ?
i'm working on :
python version : 3.10.0 and openCv version : 4.7.0.68

Trying to run python script using OpenCV on Replit, throws ImportError: OpenCV loader: missing configuration file: ['config.py']

Trying to implement OCR into my simple python program, but Replit is being weird as usual with the module requirements. I get multiple errors, but the following shows first.
I tried to run this code:
import cv2
import pytesseract
import numpy as np
img = cv2.imread('/Users/marius/Desktop/jimdoo.png')
#Alternatively: can be skipped if you have a Blackwhite image
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray, img_bin = cv2.threshold(gray,128,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
gray = cv2.bitwise_not(img_bin)
kernel = np.ones((2, 1), np.uint8)
img = cv2.erode(gray, kernel, iterations=1)
img = cv2.dilate(img, kernel, iterations=1)
out_below = pytesseract.image_to_string(img)
print("OUTPUT:", out_below)
Not familiar with OCR so I can't MRE (sorry).
But this along with many other errors occured:
raise ImportError('OpenCV loader: missing configuration file: {}. Check OpenCV installation.'.format(fnames))
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.
Not very good at programming, what does this mean? Much thanks.

How to extract text from webcam image

I am trying to extract text from the image taken by webcam. My code is below :
from PIL import Image
from pytesseract import image_to_string
import cv2
import time
cap = cv2.VideoCapture(0)
time.sleep(3)
ret, frame = cap.read()
if ret != True:
raise ValueError("Can't read frame")
cv2.imwrite('img2.png', frame)
cv2.imshow("img1", frame)
cv2.waitKey()
cv2.destroyAllWindows()
img = Image.open('img2.png')
text = image_to_string(img, lang='eng')
print(text)
I could extract text from screen shot image and now trying same thing in terms of webcam.
I just attempted to take photo with webcam and the download image taken by webcam and used image_to_string() to extract text from that image.
But it does not return any text even though the image is correctly taken.
How should I edit this ?
Otherwise, if you have any other good idea, I would like to know.

Unable to read video file using cv2.VideoCapture() function in OpenCv

import numpy as np
import cv2
cap = cv2.VideoCapture("1.mp4")
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Its throwing error
libv4l2: error getting capabilities: Inappropriate ioctl for device
VIDEOIO ERROR: V4L: device 1.mp4: Unable to query number of channels
after compiling.

OpenCV cv2 not working in Windows 7

I have a Windows 7 SP1 64 Bit machine with open cv2.4.9 installed and python 2.7.6 installed.
I use pre compiled version of opencv
The following code works perfectly for me
import cv2.cv as cv
import time
cv.NamedWindow("camera", 0)
capture = cv.CaptureFromCAM(0)
while True:
img = cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10) == 27:
break
cv.DestroyAllWindows()
Now when I try to use this code
import cv2
import numpy as np
cam = cv2.VideoCapture(0)
s, img = cam.read()
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
while s:
cv2.imshow( winName,img )
s, img = cam.read()
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
print "Goodbye"
The window is opened , the camera is initialized (as camera lights are on) , but nothing is displayed and the window closes and the program exits.
WHERE am I going wrong??
QUESTION 2
Can any one also suggest me how to capture live video stream from my Linux machine 192.168.1.3 . The stream is being generated by ffmpeg.
The video stream can be opened in web browser. But I want to capture it with opencv and python.

Categories