Using keys to toggle functions in OpenCV- Python - python

I am running into thread error :
QObject::moveToThread: Current thread (0x7c2d90) is not the object's thread (0xcc44d0).
Cannot move to target thread (0x7c2d90)
while trying to run the below program. when i take off the last two lines, it doesn't give me this error. Can anybody tell why this is happening?
import sys
import cv2
fileName = sys.argv[1]
print "image read from commandline"
img = cv2.imread(fileName)
print "image sucessfully read"
image = cv2.imread('testimage1.jpg', 0)
cv2.imshow('image', image )
cv2.waitKey(0)

I was using Anaconda package , now i switched to python 2.7 distribution and the error is gone.

Related

Python Mediapipe module call

mediapipe==0.8.10.1
I'm trying to make a project based on mediapipe, and I've installed the package normally using
pip install mediapipe
I'm running this in the VS Code editor within a venv which has access to global packages,
import cv2
import mediapipe as mp
# why do I need to do this, instead of simply
# "mpHands = mp.solutions.hands"
# to access the hands function because otherwise it doesn't recognize the module
mpHands = mp.solutions.mediapipe.python.solutions.hands
Hands = mpHands.Hands() # doesn't recognize unless I do the entire line above
capture = cv2.VideoCapture(0)
while True:
ret, img = capture.read()
cv2.imshow("res", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
From the google github and other tutorials, "mp.solutions.hands" would be the usual way to call the module, but after some head scratching I've noticed that I need to use "mp.solutions.mediapipe.python.solutions.hands"
to access the "Hands()" module, otherwise it's unrecognized.
Moreover this only happens with mediapipe and not with, for example opencv.
I'm really not sure why this happens; I am new to package handling so any advice would be appreciated,
thank you.

read data matrix with python in raspberry pi using pylibdmtx package

I'm using libdmtx to decode data matrix with rasbian OS. when i run my code in windows, i get perfect results but in linux it does'nt work.
here is my simple code, when i run it in linux the variable "code" is always empty. i dont have any error. what is the problem?
import cv2
import time
from pylibdmtx.pylibdmtx import decode
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
while True:
ret,img = cap.read()
cv2.waitKey(1)
t0 = time.time()
code = decode(img, timeout=100, max_count=1, corrections=3)
if(code):
print((time.time() - t0)*1000)
print(code)
print(code[0].data.decode('utf-8'))
print(code[0].rect)
#x,y,w,h = code[0].rect
#cv2.rectangle(img,(x,y),(x+w,480-y-h),(255,0,255),2)
cv2.imshow('Result',img)
if cv2.waitKey(1) == 27:
break
Check to make sure you've installed libdmtx on Linux:
From the repository's readme:
"The libdmtx DLLs are included with the Windows Python wheels. On other operating systems, you will need to install the libdmtx shared library."
That'd be my first guess.
code = decode(img, timeout=100, max_count=1, corrections=3)
the timeout is to low. Try without timeout
code = decode(img, max_count=1, corrections=3)

Process finished with exit code 134 (interrupted by signal 6: SIGABRT) in Python and OpenCV

Consider:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I'm using Python and OpenCV in the PyCharm IDE. When I try to open the webcam using OpenCV, it gives the following error:
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
Is this happening because I'm running out of memory?
What are the solutions for this?
I'm using PyCharm on MacBook Pro (OS: macOS v10.14 (Mojave)).
Open /Applications/PyCharm.app/Contents/info.plist,
Insert a new line:
Key: Privacy - Camera Usage
Type: String
Value: An application in PyCharm wants to use the camera.
Save it.
Reopen PyCharm.
Run your code.
I have opened an issue at JetBrains because of this. But here is a workaround:
Run PyCharm or IntelliJ IDEA (whatever JetBrains application) from an application which already has been approved for camera access. For example, I use the Hyper terminal to run the IDE and everything works.
By running the Python script in Visual Studio Code, it will execute the program without any warnings or bugs.
Open file /Applications/PyCharm.app/Contents/info.plist in any text editor.
Add these two lines before the dict and plist tags:
<key>Privacy - Camera Usage</key>
<string>An application in PyCharm wants to use the camera.</string>
</dict>
</plist>

opencv cv2.destroyAllWindows() doesnt respond

I have 64bit, ubuntu system. Im running the code in idel.
I was facing opencv hang issue , the image shows up but I have to force kill the image window. So reffered to this thread -- Using other keys for the waitKey() function of opencv
import cv2
img = cv2.imread('sof.jpg') # load a dummy image
while(1):
cv2.imshow('img',img)
k = cv2.waitKey(3000) & 0xff
if k==32: # SpaceBar key to stop
break
elif k==-1: # normally -1 returned,so don't print it
continue
else:
print k # else print its value
Still its not working image hangs and I have to close it manually.
try :
k = cv2.waitKey(3000) & 0xff
Add cv2.waitkey(0) and cv2.destroyallwindows() but If you have used python notebooks then there is a problem in Unix based system to run a program of opencv. It will cause system freeze so you will need to restart kernel everytime when you try to execute code.
I have an alternative method which would prevent from freezing your system
Steps:
-Copy the code from python notebooks and create new filename.py and paste it
Open terminal
cd path/to/file
source activate VirtualEnvironment
python filename.py
This will run code directly from terminal.
Hope this helps you.
Example Link: https://youtu.be/8O-FW4Wm10s

Error while trying to save webcam picture with OpenCV

import cv
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
cv.SaveImage("test.JPG", img)
Hi,
I just want to save a picture from my webcam with OpenCv and Python on my Ubuntu 10.
OpenCv can connect with the webcam.
But I get this error:
OpenCV Error: Null pointer (NULL array pointer is passed) in cvGetMat, file /build/buildd/opencv-2.1.0/src/cxcore/cxarray.cpp, line 2376
Traceback (most recent call last):
File "video.py", line 5, in <module>
cv.SaveImage("test.JPG", img)
cv.error: NULL array pointer is passed
Save yourself a trip to the emergency room and use SimpleCV. It's a Pythonic wrapper for OpenCV's Python bindings and a few more tools (it uses Numpy, Scipy and PIL):
from SimpleCV import *
camera = Camera()
image = camera.getImage()
image.save('test.JPG')
I see this mistake over and over and over and over again: the CaptureFromCAM() call is failing, which means that QueryFrame() is failing as a consequence and returning NULL as image, causing SaveImage() to fail as well.
Two things that you need to take into consideration here:
1) your webcam might not be index 0 (try -1, or 1)
2) learn to code safely! Always check the return of the functions that are being called. This practice will save you a lot of time in the future:
capture = cv.CaptureFromCAM(0)
if not capture:
// deal with error, return, print a msg or something else.
img = cv.QueryFrame(capture)
if not img:
// deal with error again, return, print a msg or something else entirely.
cv.SaveImage("test.JPG", img)

Categories