Opencv Python-3.5.2 Face Recognition - opencv

I am working on Face Recognition. My code was working fine but the results were not that good so I did a little revision and along with some amendements, I updated my opencv-python from 3.4.3 to 3.4.4 and same for the opencv-contrib-python. And now I am getting syntax errors.
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Error: cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
And also,
cv2.face.LBPHFaceRecognizer_create()
AttributeError: module 'cv2.cv2' has no attribute 'face'
I didn't get any Errors like this before but after updating, I am getting these. What's the solution.

You can go back to the previous version and try.
pip uninstall opencv-python
pip install opencv-python==3.4.3
the same as opencv-contrib-python.
Then see if your code works.

Related

Issue when loading ONNX model with OpenCV

I trained a custom YOLOv7 object detection model in Google Colab. I exported it to .onnx file using this command:
python export.py --weights runs/train/exp/weights/best.pt --grid --end2end --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.3 --img-size 640 640 --max-wh 640
This is my code to load the model:
import cv2
net = cv2.dnn.readNet('runs/train/exp/weights/best.onnx')
When I run the code above, I get this error:
error: OpenCV(4.6.0) /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1040: error: (-2:Unspecified error) in function 'handleNode'
> Node [NonMaxSuppression#ai.onnx]:(onnx_node!NonMaxSuppression_370) parse error: OpenCV(4.6.0) /io/opencv/modules/dnn/src/layer_internals.hpp:110: error: (-2:Unspecified error) Can't create layer "onnx_node!NonMaxSuppression_370" of type "NonMaxSuppression" in function 'getLayerInstance'
However, this python code loads the model successfully:
import onnx
onnx_model = onnx.load('runs/train/exp/weights/best.onnx')
onnx.checker.check_model(onnx_model)
So seems like the issue is in OpenCV?
The .onnx file is in the correct directory.
Enviroment:
opencv-python: 4.6.0.66
torch: 1.11.0
onnx: 1.11.0
CUDA: 11.2
My PyTorch was previously version 1.12.0, but I got similar issues too. Then I read somewhere that downgrading to 1.11.0 should fix my problem. It didn't.
I downloaded a pre-exported .onnx file from google and it loaded successfully using the same code. Why can't I do it now?

cv2.dnn.readNetFromDarknet error: (-212:Parsing error) Unsupported activation: relu in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'

I tried to run the Openpose on darknet with weights and cfg downloaded from this place: https://github.com/lincolnhard/openpose-darknet
This is the error when I tried to create a net in Opencv
modelConfiguration = path to cfg file
modelWeights = path to weights file
darknet = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
error Traceback (most recent call last)
in ()
----> 1 darknet = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\dnn\src\darknet\darknet_io.cpp:552: error: (-212:Parsing error) Unsupported activation: relu in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'
The fix is either you install the latest master branch of openCV or OpenCV version 3.4.XX.XX Only these branches support yolo4.
You can install another version of OpenCV by the following snippet:
!pip install opencv-contrib-python==3.4.13.47 --force-reinstall
as you konw, opnecv 4.0.0 does not support the relu activation now.
I found some project faced the same problem, but he adjusted opencv for darknet, Support CNN "relu" and maxpool "VALID", you can reference it.
https://github.com/chineseocr/opencv-for-darknet
hope it will solve your problem.

YoloV3 : undefined symbol: _ZN9_IplImageC1ERKN2cv3MatE when Build with OpenCV

I use https://github.com/AlexeyAB/darknet to run Yolo V3 and darknet is built using OpenCV 3.4.0.
When I tried to run this command ./darknet detector train data/obj.data cfg/objyolov3.cfg darknet53.conv.74 to train my own dataset, with OPENCV = 0 training works successfully, but when I build with OPENCV=1, I got this following error :
Loading weights from darknet53.conv.74...
seen 64
Done!
Learning Rate: 0.001, Momentum: 0.9, Decay: 0.0005
If error occurs - run training with flag: -dont_show
Resizing
896 x 896
./darknet: symbol lookup error: ./darknet: undefined symbol: _ZN9_IplImageC1ERKN2cv3MatE
The chart image showed up but closed immediately, adding -dont_show also not helping. I couldn't find similar problem on the internet, any solution for this?
I know this is very rare to encounter this problem. But I managed to solve it after discussed it with the repo's author. Insted building darknet using make, it works when I built darknet using cmake.
Edit CMakeLists.txt in darknet folder and add this line : SET(OpenCV_DIR /home/<Change this to your OPENCV path installation>/OpenCV-3.4.0/share/OpenCV/)
cmake .
make
Original github issue : https://github.com/AlexeyAB/darknet/issues/2489

"'cv2.face' has no attribute 'LBPHFaceRecognizer_create'" but if installed from pip the gui gives qt thread error

I'm trying to run a python script for a basic face recognition but when running after installing from pip I get the error:
QObject::moveToThread: Current thread (0x24c8e90) is not the object's thread (0x20da6d0).
Cannot move to target thread (0x24c8e90)
I found this question and it solved the problem but after that I get this error:
Traceback (most recent call last):
File "face_recognition.py", line 21, in
recognizer = cv2.face.LBPHFaceRecognizer_create()
AttributeError: module 'cv2.face' has no attribute 'LBPHFaceRecognizer_create'
I found many answers (for example) to this problem but all suggested to install the opencv-contrib-python package from pip.
How can I include the needed package from apt-get?
The code where I get the error:
import cv2
import numpy as np
recognizer = cv2.face.LBPHFaceRecognizer_create()

OpenCV ImportError using python

I'm trying to work with OpenCV3 in Python 3.5. I installed it but when I call
import cv2
I get this error message:
ImportError: /usr/lib/x86_64-linux-gnu/libmirprotobuf.so.3: undefined
symbol:
_ZNK6google8protobuf11MessageLite25InitializationErrorStringB5cxx11Ev
Anybody ever had this error? Thanks.

Resources