How to extract text from webcam image - python

I am trying to extract text from the image taken by webcam. My code is below :
from PIL import Image
from pytesseract import image_to_string
import cv2
import time
cap = cv2.VideoCapture(0)
time.sleep(3)
ret, frame = cap.read()
if ret != True:
raise ValueError("Can't read frame")
cv2.imwrite('img2.png', frame)
cv2.imshow("img1", frame)
cv2.waitKey()
cv2.destroyAllWindows()
img = Image.open('img2.png')
text = image_to_string(img, lang='eng')
print(text)
I could extract text from screen shot image and now trying same thing in terms of webcam.
I just attempted to take photo with webcam and the download image taken by webcam and used image_to_string() to extract text from that image.
But it does not return any text even though the image is correctly taken.
How should I edit this ?
Otherwise, if you have any other good idea, I would like to know.

Related

Tesseract doesn't recognize certain pictures. Python

Tesseract works fine when I use other pictures but whenever I use this picture it doesn't recognize the picture.
Can someone explain me why please?
import cv2
import pytesseract
import time
import random
from pynput.keyboard import Controller
keyboard = Controller() # Create the controller
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = cv2.imread("capture5.png")
#img = cv2.resize(img, (300, 300))
cv2.imshow("capture5", img)
text = pytesseract.image_to_string(img)
print(text)
cv2.waitKey(0)
cv2.destroyAllWindows()
I fixed my problem, all I needed to do was add this code to my script.
text = pytesseract.image_to_string(
img, config=("-c tessedit"
"_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
" --psm 10"
" "))

Unable to detect text from image python opencv

I am trying to detect text from image of a particular font i also have font name and its ttl file and i also try to resize the file but still i am unable to do that here is code :
import cv2
import pytesseract
def read(image):
img=cv2.imread(image)
img = cv2.resize(img, None, fx=2, fy=2)
text=pytesseract.image_to_string(image=img)
text = text.strip()
return text
print(read('data.png'))
Where data.png is:
And font name is Monotype Corsiva Regular
So is there any ways to read the text ?

Py: How to extract text with hashtag from image

I am trying to extract "#opentowork" text from LinkedIn account thumbnails but it does not seem to work.
the image is:
I have tried the simplest way:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
print(pytesseract.image_to_string(r'1633367686467.jpg'))
And using opencv
img = cv2.imread('1516535608688.jpg', 0)
# img = 255 - img #invert image
print(pytesseract.image_to_string(img, lang='eng', config = '--psm 12'))
But it returns nothing. I am not really experienced in text recognition. Is it possible to extract the text from such images?

How to display frames of a video file as gif in Jupyter notebook without writing file

How do I read in a video file and show frames of that video as a gif animation in a Jupyter notebook without writing to file?
This answer combines multiple answers about reading a video file and converting it in memory to a gif and how to display this bytes object inside a Jupyter environment. I'm sorry, but I couldn't find again all the resources I used.
import imageio
import skimage
import cv2
from IPython.display import Image
from io import BytesIO
file = "video.mkv"
capture = cv2.VideoCapture(file)
frames = list()
for frame_no in range(10):
capture.set(1, frame_no)
ret, frame = capture.read()
assert ret
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frames.append(frame)
gif_file = BytesIO()
imageio.mimsave(gif_file, [skimage.img_as_ubyte(frame) for frame in frames], 'GIF', fps=2)
Image(data=gif_file.getvalue())

OpenCV cv2 not working in Windows 7

I have a Windows 7 SP1 64 Bit machine with open cv2.4.9 installed and python 2.7.6 installed.
I use pre compiled version of opencv
The following code works perfectly for me
import cv2.cv as cv
import time
cv.NamedWindow("camera", 0)
capture = cv.CaptureFromCAM(0)
while True:
img = cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10) == 27:
break
cv.DestroyAllWindows()
Now when I try to use this code
import cv2
import numpy as np
cam = cv2.VideoCapture(0)
s, img = cam.read()
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
while s:
cv2.imshow( winName,img )
s, img = cam.read()
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
print "Goodbye"
The window is opened , the camera is initialized (as camera lights are on) , but nothing is displayed and the window closes and the program exits.
WHERE am I going wrong??
QUESTION 2
Can any one also suggest me how to capture live video stream from my Linux machine 192.168.1.3 . The stream is being generated by ffmpeg.
The video stream can be opened in web browser. But I want to capture it with opencv and python.

Categories