python version 3.6.1
numpy version 1.13.1
OpenCV version 3.3.0
import numpy as np
import cv2
import os
from PIL import Image
recognizer = cv2.createLBPHFaceRecognizer()
path = 'dataset'
def getImageWithID(path):
imagesPaths = [os.path.join(path,f) for f in os.listdir(path)]
faces = []
IDs = []
for imagePath in imagePaths:
faceImg = Image.open(imagePath)
faceNp = np.array(faceImg, 'uint8')
ID = int(os.path.split(imagePath)[-1].split(".")[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow("training the data ", facesNp)
cv2.waitKey(10)
return IDs, faces
Ids, faces = getImageWithID(path)
recognizer.train(faces, Ids)
recognizer.save("recognizer/trainingData.yml")
cv2.destroyAllWindows()
AttributeError: module 'cv2.face' has no attribute 'createLBPHFaceRecognizer
'
Your problem is you are using Opencv 3.x. Try cv2.face.LBPHFaceRecognizer_create() for the said problem.
But if still compatibility issue persists then either add opencv_contrib module to your existing opencv 3.x version or change the opencv to older version.
Related
I have been trying to implement this piece of code and realized a lot has been deprecated and updated in TensorFlow V2.
CODE
filenames=['images/000001.jpg','images/000002.jpg','images/000003.jpg','images/000004.jpg']
labels=[1,0,1,0]
filename_queue=tf.train.string_input_producer(filenames)
reader=tf.WholeFileReader()
filename, content = reader.read(filename_queue)
images=tf.image.decode_jpeg(content, channels=3)
images=tf.cast(images, tf.float32)
resized_images=tf.image.resize_images(images, (224, 224))
I have altered the tf.train.string_input_producer(filenames) part. But I am not able to find any alternative to tf.WholeFileReader(). Also I am using google colab and thus cannot rely on V1 as it says colab don't support TensorFlow V2.
Here's my piece of code.
My code
import os
import numpy as np
image_path = '/content/drive/MyDrive'
categ = ["IMG"]
for c in categ:
path = os.path.join(image_path, c)
for img in os.listdir(path):
image = [os.path.join(path, img)]
queue=tf.data.Dataset.from_tensor_slices(image)
reader=tf.WholeFileReader() #AttributeError
Can anyone help?
You probably should be able to find it here:
tf.io.WholeFileReader()
instead of
tf.WholeFileReader()
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]
I have come across this link for face detection and image cropping. I would like to use this script but I have cv2 install and only import cv2 works but not import cv.
How can I convert the cv functions in the following function to cv2 functions?
def faces_from_pil_image(pil_image):
"Return a list of (x,y,h,w) tuples for faces detected in the PIL image"
storage = cv.CreateMemStorage(0)
facial_features = cv.Load('haarcascade_frontalface_alt.xml', storage=storage)
cv_im = cv.CreateImageHeader(pil_image.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(cv_im, pil_image.tostring())
faces = cv.HaarDetectObjects(cv_im, facial_features, storage)
# faces includes a `neighbors` field that we aren't going to use here
return [f[0] for f in faces]
Either use
import cv2
storage = cv2.cv.CreateMemStorage(0)
or
from cv2 import *
storage = cv.CreateMemStorage(0)
I have installed cv2 (opencv-3.0.0) on my Windows machine, but unable to access MSER class:
import cv2
cv2.MSER()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MSER'
For cv2 installation, I downloaded and extracted opencv-3.0.0.0.exe and then copied cv2.pyd to Ananconda sitepackage directory.
I see couple of post on internet about using cv2.MSER, but I can't figure out what is the issue with my cv2.
I was refering opencv-2.4 way of using MSER. From opencv-3.0.0 documentation, it appears that I need to use following:
cv2.MSER_create()
cam = video.create_capture(video_src)
mser = cv2.MSER_create()
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
regions = mser.detectRegions(gray, None)
I'm new to python and open cv. I'm trying to find out how to load an image in opencv with python. Can any one provide an example (with code) explaining how to load the image and display it?
import sys
import cv
from opencv.cv import *
from opencv.highgui import *
ll="/home/pavan/Desktop/iff pics/out0291.tif"
img= cvLoadImage( ll );
cvNamedWindow( “Example1”, CV_WINDOW_AUTOSIZE );
cvShowImage( “Example1”, img );
cvWaitKey(10);
cvDestroyWindow( “Example");
There have been quite a few changes in the openCV2 API:
import cv
ll = "/home/pavan/Desktop/iff pics/out0291.tif"
img = cv.LoadImage(ll)
cv.NamedWindow("Example", cv.CV_WINDOW_AUTOSIZE )
cv.ShowImage("Example", img )
cv.WaitKey(10000)
cv.DestroyWindow("Example")
It is a simpler, quite cleaner syntax!
Also, you don't need trailing ; à-la-matlab. Last, be careful about the quotes you use.
For the newer openCV3 API, you should see the other answer to this question.
import cv2
image_path = "/home/jay/Desktop/earth.jpg"
img = cv2.imread(image_path) # For Reading The Image
cv2.imshow('image', img) # For Showing The Image in a window with first parameter as it's title
cv2.waitKey(0) #waits for a key to be pressed on a window
cv2.destroyAllWindows() # destroys the window when the key is pressed
There are 2 possible approaches to this:
Using argparse (recommended):
import cv2
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,help = "Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
This will take the image as an argument, will then convert the argument, add it to ap and the load it using the imread function()
To run it.
Go to your required folder
source activate your environment
python filename.py -i img.jpg
Hardcoding the image location:
import cv2
img = cv2.imread("\File\Loca\img.jpg")
cv2.imshow("ImageName",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run this similarly, omitting the arguments.