I intend to use opencv and pytesseract to extract text of out images. On executing the following code in a Python 3.7 Interpreter, I am receiving a error I'm not sure I understand.
import cv2
import pytesseract
#tesseract.exe location provided
pytesseract.tesseract_cmd=r'C:\\Users\\shree\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe'
#image location provided
img=cv2.imread('C:\\Users\\shree\\Desktop\\hey.jpeg')
text=pytesseract.image_to_string(img)
print(text)
error shown
P.S:
I have installed opencv and pytessearct in
"C:\Users\shree\AppData\Local\Programs\Python\Python37-32\Scripts"
Python 3.7 Interpreter is installed in
"C:\Users\shree\AppData\Local\Programs\Python\Python37-32"
Please excuse if this is an obvious/dumb question. Actually, I'm just starting out. I read about this at https://towardsdatascience.com/read-text-from-image-with-one-line-of-python-code-c22ede074cac
A Detailed answer and an alternate way(if required) would be highly appreciated.
Thanks!
Replace the line,
pytesseract.tesseract_cmd=r'C:\\Users\\shree\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe'
with,
pytesseract.pytesseract.tesseract_cmd = r'C:\\Users\\shree\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe'
This should fix the issue if tesseract is installed properly.
I'm trying to write an app using python and PIL that involves processing frames from my macbook camera. Is there an easy way to capture frames from the iSight for use with PIL without installing too many libraries?
I've seen a few questions on SO about doing this with OpenCV, which I don't want to do. Can I do this without using OpenCV as I've had trouble installing that?
Maybe you can try using the SimpleCV library. After installing it, the following code should work:
from SimpleCV import Camera
from SimpleCV import Image
webcam_camera = Camera()
webcam_image = webcam_camera.getImage()
webcam_image.save("frame.jpg")
Although OpenCV would be a much better option. If you follow the instructions on their website, it wouldn't be too hard to get it up and running.
I am trying to capture a video and convert into frames using python and openCV. I followed the steps that need to done to import openCV2 libraries for python in windows 8 which is as follows:
Download Python, Numpy, OpenCV from their official sites.
Extract OpenCV (will be extracted to a folder opencv)
Copy ..\opencv\build\python\x86\2.7\cv2.pyd
Paste it in C:\Python27\Lib\site-packages
Open Python IDLE or terminal, and type
I have done the above steps to import opencv libraries into python.
But when I import CV2 libraries and try to capture a video, I was unable to generate frames using the function cv.CaptureFromFile() and accessing the frames using cv.QueryFrame() functions. I was unable to capture the frames. Please find the code below.
import sys
import os
import numpy as np
import PIL
from matplotlib import pyplot as plt
import cv2.cv as cv
winname = "myWindow"
win = cv.NamedWindow(winname, cv.CV_WINDOW_AUTOSIZE)
invideo = cv.CaptureFromFile("chayya1.avi")
totalNumberOfFrames = int(cv.GetCaptureProperty(invideo, cv.CV_CAP_PROP_FRAME_COUNT))
framesprocessing = totalNumberOfFrames
print(totalNumberOfFrames)
while (True):
im = cv.QueryFrame(invideo)
cv.ShowImage(winname, im)
if cv.WaitKey() == 27: # ASCII 27 is the ESC key
break
del invideo
cv.DestroyWindow(winname)
The output is getting processed without any errors but the frames are not generating and even the print statement for frame count is also zero.
I just want to what is procedure for installing OpenCV for python in windows and accessing the frames of a video using the obove mentioned functions.
Thanks in advance
Using opencv to read video files
As mentioned in the tutorial here, the recommended approach now is to use cv2 (not cv as you did) to read video files using, e.g.
import numpy as np
import cv2
cap = cv2.VideoCapture("chayya1.avi")
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Python & OpenCV installation on windows
For a simple solution to installing OpenCV's python bindings on windows you might want to try the Python(xy) distribution which bundles a bunch of python modules including opencv.
Python's opencv bindings are included with opencv, so if you didn't get any import errors then your opencv setup is likely to be correct.
Alternative bindings
If you don't like the native bindings, you could try using pip from the command line to install 3rd party bindings, though as berak pointed out this is not generally recommended;
pip install pyopencv
Pip itself can be installed as outlined in this answer
I am trying to run the simplest opencv SIFT code through the shell of Ubuntu, with no luck
I get an error:
AttributeError: 'module' object has no attribute 'SURF'
The code:
import cv2
cv2.SIFT()
My configurations:
Ubuntu version is 13.10 64bit
cv2.__version__ is 2.4.5
the output of dir(cv2) is (for the S letter only)
'scaleAdd', 'segmentMotion', 'sepFilter2D', 'setIdentity',
'setMouseCallback', 'setTrackbarPos', 'setUseOptimized',
'setWindowProperty', 'solve', 'solveCubic', 'solvePnP',
'solvePnPRansac', 'solvePoly', 'sort', 'sortIdx', 'split', 'sqrt',
'startWindowThread', 'stereoCalibrate', 'stereoRectify',
'stereoRectifyUncalibrated', 'subtract', 'sumElems'
This was driving me crazy but scratch all the other suggestions, turns out you can now get SIFT and SURF with just two terminal commands!
Be sure there is no other opencv on you computer...
pip uninstall opencv-python
Then get the contribute version (has SIFT and SURF + others)...
pip install opencv-contrib-python
It should install but note that the names are a little different.
import cv2
sift = cv2.xfeatures2d.SIFT_create()
!!!pip pip hurray!!! (that's just a pun, not part of the code)
import cv2
sift = cv2.SIFT()
This code will not work if you are using opencv version 3.0 or greater.
an alternative of this code is
sift = cv2.xfeatures2d.SIFT_create()
(Only works if you have installed opencv-contrib-python library )
Now again if you have an opencv-contrib-python version > 3.4
than it won't work with a different erro
error: OpenCV(4.1.0)
C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1207:
error: (-213:The function/feature is not implemented) This algorithm
is patented and is excluded in this configuration; Set
OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function
'cv::xfeatures2d::SIFT::create'
best solution for this is:
**step 1: pip uninstall opencv-python**
**step 2: pip install opencv-contrib-python==3.4.2.16**
This worked for me.
[Note : If you have not installed opencv using pip install opencv-python, than just go and delete the downloaded library and follow the above instruction]
Not the smoothest way to do it, but it worked for me.
#Berak explained to me, as you can observe in the comments on my question, that the SIFT algorithm, as well as FAST algorithm are patented, which means that they are not part of the regular opencv installation.
Therefore I searched for a python distribution that will have it all - and I found one. So, I didn't actually solved the problem, as #Berak suggested, alternatively I bypassed it using Python(x,y)
Thanks for the help,
Ozrad
I also had problem in using SIFt because i had only openCV. But after installing ROS Hydro, i am able to use SIFT/SURF as they come under nonfree part.
A simple code i found for SIFT
import cv2
import numpy as np
img = cv2.imread('home.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT()
kp = sift.detect(gray,None)
img=cv2.drawKeypoints(gray,kp)
cv2.imwrite('sift_keypoints.jpg',img)
And i tested the code, it works
I followed the openCV python windows installation guide. I went to try using cv2.SIFT and found it was not available in this installation.
After completely removing python 2.7 and openCV, I then installed python(x,y) and included openCV. I get
cv2.version
'2.4.8'
And:
cv2.SIFT ->
cv2.SURF
So python(x,y) does include SIFT,SURF modules.
You can use it easily as follows:
sift = cv2.xfeatures2d_SIFT()
keypoints_detector = sift.detect(image=your_grayscale_image, mask=None)
Both the SIFT and SURF are pattented algorithms by their respective authors. Previously they were not available in the main repository of OpenCV but in contrib but now as per OpenCV, their patent has been expired in 2020 hence SIFT and SURF are added in the main repository in latest releases. I have tried 4.5.2 and it works fine.
You can install this opencv version using pip3 install opencv-python=4.5.2.
To instantiate and test SIFT use the below code.
import numpy as np
import cv2 as cv
img = cv.imread('home.jpg')
gray= cv.cvtColor(img,cv.COLOR_BGR2GRAY)
sift = cv.SIFT_create()
kp = sift.detect(gray,None)
img=cv.drawKeypoints(gray,kp,img)
cv.imwrite('sift_keypoints.jpg',img)
Use a version above 4.4.0, SIFT was moved into main lib again.
https://opencv.org/opencv-4-4-0/
pip install opencv-python==4.4.0.46
import cv2
sift = cv2.SIFT_create()
I'm wondering if there's a get rope-code-assist working on the cv namespace of openCV
import cv2.cv as cv
cv.LoadImage
So far I get [no match]. Is there something else I need to do?
The cv2.pyd is in the PYTHONPATH and has been built from the opencv source.