Syntax error in importing a python package - python

I wanted to use the flask-opencv-streamer python package but cannot seem to find a workaround on how to import it.
If i use the example code at here: https://pypi.org/project/flask-opencv-streamer/#description , I will get an import error and in case if I change the '_' to '-',it gives syntax error.
from flask-opencv-streamer.streamer import Streamer
import cv2
port = 3030
require_login = False
streamer = Streamer(port, require_login)
# Open video device 0
video_capture = cv2.VideoCapture(0)
while True:
_, frame = video_capture.read()
streamer.update_frame(frame)
if not streamer.is_streaming:
streamer.start_streaming()
cv2.waitKey(30)

Related

AttributeError: module 'cv2' has no attribute 'face' OpenCV 4.7.0

After referring to some Stack Overflow answers I did pip install opencv-contrib-python, still I am getting those errors.
I am using OpenCV 4.7.0.
This is for a facial recognition project tutorial that I am following.
import cv2
import numpy as np
from PIL import Image
import os
# Path for face image database
path = 'dataset'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
# function to get the images and label data
def getImagesAndLabels(path):
imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
faceSamples=[]
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # grayscale
img_numpy = np.array(PIL_img,'uint8')
id = int(os.path.split(imagePath)[-1].split(".")[1])
faces = detector.detectMultiScale(img_numpy)
for (x,y,w,h) in faces:
faceSamples.append(img_numpy[y:y+h,x:x+w])
ids.append(id)
return faceSamples,ids
print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces,ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
# Save the model into trainer/trainer.yml
recognizer.write('trainer/trainer.yml')
# Print the numer of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))
I keep getting this error:
File "/Users/sashuponnaganti/workspace/Facial Recognition Project/face_trainer.py", line 7, in <module>
recognizer = cv2.face.LBPHFaceRecognizer_create()
AttributeError: module 'cv2' has no attribute 'face'
Any ideas how to fix this?
I already tried doing pip install opencv-contrib-python and I had already installed it so it made no difference.
This question has been solved. I had installed all my opencv packages in a conda environment but was running it with the wrong interpreter.

I am trying to make a video chat-app using imagezmq and opencv

Here is the code:
Server:
import cv2
import numpy as np
import imagezmq
image_hub = imagezmq.ImageHub()
while True: # show streamed images until Ctrl-C
win_name = "Feed"
image = image_hub.recv_image()
img = cv2.imdecode(image, 1)
cv2.imshow(win_name, img)
cv2.waitKey(1)
image_hub.send_reply(b'OK')
Client:
import socket
import time
from imutils.video import VideoStream
import imagezmq
import cv2
import numpy as np
sender = imagezmq.ImageSender(connect_to='tcp://192.168.0.12:5555')
vs = VideoStream(src=0).start()
time.sleep(2.0)
while True: # send images as stream until Ctrl-C
frame = vs.read()
img_arr = np.array(bytearray(frame))
sender.send_image("Img",img_arr)
And i get the error:
img = cv2.imdecode(image, 1)
TypeError: Expected Ptr<cv::UMat> for argument 'buf'
on the server
I have checked online but could not find a suitable answer
Note: i am not that experienced in this.
according to imagezmq's documentation, one does not use any imdecode, and one does not use any bytearray() calls.
you just send numpy arrays back and forth.
further, I would advise against using imutils, in particular the VideoStream wrapper. use OpenCV's VideoCapture. check for isOpened() before trying to read. when reading with ok, frame = cap.read(), check if the read frame is ok, otherwise break the reading loop and discard the value in frame.

CAP_PROP_FRAME_COUNT constant is missing in opencv `cv2` python module

How to access to CAP_PROP_FRAME_COUNT from opencv in python?
I tried this:
import cv2
cap = cv2.VideoCapture('myvideo.avi')
frames_count, fps, width, height = cap.get(cv2.CAP_PROP_FRAME_COUNT), cap.get(cv2.CAP_PROP_FPS), cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
And this:
import cv2
import cv
cap = cv2.VideoCapture('myvideo.avi')
frames_count, fps, width, height = cap.get(cv.CAP_PROP_FRAME_COUNT), cap.get(cv.CAP_PROP_FPS), cap.get(cv.CAP_PROP_FRAME_WIDTH), cap.get(cv.CAP_PROP_FRAME_HEIGHT)
and also this:
import cv2
cap = cv2.VideoCapture('myvideo.avi')
frames_count, fps, width, height = cap.get(cv2.cv.CAP_PROP_FRAME_COUNT), cap.get(cv2.cv.CAP_PROP_FPS), cap.get(cv2.cv.CAP_PROP_FRAME_WIDTH), cap.get(cv2.cv.CAP_PROP_FRAME_HEIGHT)
But I'm getting this error:
AttributeError: 'module' object has no attribute 'CAP_PROP_FRAME_COUNT'
I'm using python 2.7.5 and OpenCV 2.4.9.
The constants in the first version of OpenCV python module have a CV_ prefix. You could thus either use cv.CV_CAP_PROP_FRAME_COUNT or cv2.cv.CV_CAP_PROP_FRAME_COUNT.
While running macports on OSX (opencv #3.0.0_1+python27+tbb)
You can get CAP_PROP_FRAME_HEIGHT and CAP_PROP_FRAME_WIDTH with the following:
#!/opt/local/bin/python
import cv2
vcap = cv2.VideoCapture()
# set frame width and height
vcap.set(cv2.CAP_PROP_FRAME_WIDTH, 480)
vcap.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
vcap.open(0)
In OpenCV 2.x, these attributes are named starting with CV_... like CV_CAP_PROP_FRAME_COUNT.
In OpenCV 3.x and OpenCV 4.x, these attributes are named without CV_... like CAP_PROP_FRAME_COUNT.
http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get
#3. CV_CAP_PROP_FRAME_WIDTH
print "\t CAP_PROP_FRAME_WIDTH: ",cap.get(3)
#4. CV_CAP_PROP_FRAME_HEIGHT
print "\t CAP_PROP_FRAME_HEIGHT: ",cap.get(4)
http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-set
#3. CV_CAP_PROP_FRAME_WIDTH
cap.set(3,320)
#4. CV_CAP_PROP_FRAME_HEIGHT
cap.set(4,240)
import cv2
import cv2.cv as cv
Using cv2:
stream = cv2.VideoCapture(filename)
print stream.get(cv.CV_CAP_PROP_FRAME_COUNT)

How to make this code work with py2exe

My code is like this:
from PIL import Image
from VideoCapture import Device
def cam_capture(usr):
try:
cam = Device()
cam.saveSnapshot('%s//intruder.jpg'%(usr))
del cam
except:
return
if __name__=='__main__':
cam_capture('aninda')
the problem is when i execute this code it works just fine but when i make an exe with
py2exe then it doesn't save the image..I am on windows 7 and using python 2.7

importing opencv moduls

I have a simple code as mentioned below:
import cv
from opencv.cv import *
from opencv.highgui import *
img = cv.LoadImage("test.jpg")
cap = cv.CreateCameraCapture(0)
while cv.WaitKey(1) != 10:
img = cv.QueryFrame(cap)
cv.ShowImage("cam view", img)
cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1))
But I faced to this error:
# AttributeError: 'module' object has no attribute 'LoadImage'
when I change the code to below:
import cv
#from opencv.cv import *
#from opencv.highgui import *
img = cv.LoadImage("test.jpg")
cap = cv.CreateCameraCapture(0)
while cv.WaitKey(1) != 10:
img = cv.QueryFrame(cap)
cv.ShowImage("cam view", img)
cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1))
now the first error got solve and another error raise.
AttributeError: 'module' object has no attribute 'LoadHaarClassifierCascade'
I need both of the modules but it seems that they have conflict to gether.
Now what I have to do?
In OpenCV to load a haar classifier (in the python interface anyway) you just use cv.Load.
import cv
cascade = cv.Load('haarcascade_frontalface_alt.xml')
See the examples here.
Also, the samples that come with the OpenCV source are really good (in OpenCV-2.xx/samples/python).
How do you access the stuff you've imported?
# imports the cv module, all stuff contained in it and
# the module itself is now accessible via: cv.classname, cv.functionname
# where classname, functionname is the name of the class/function which
# the cv module provides..
import cv
# imports everything contained in the opencv.cv module
# after this import it is available via it's classname, functionname, etc.
# Attention: without prefix!!
from opencv.cv import *
# #see opencv.cv import
from opencv.highgui import *
#see python modules for more details about modules and imports in python.
If you can provide which classes are contained in which module I could add a specific solution for your problem.

Categories

Resources