I am working on a python3 project on windows 10, and I was wondering if anyone knew of anyway to pass an opencv screen grab through pytesseract? If not, is there any other OCR that you could?
Here is the code for the opencv screen grab:
import numpy as np
from PIL import ImageGrab
import cv2
while True:
screen = np.array(ImageGrab.grab(bbox=(0,40,800,640)))
cv2.imshow('window', cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
I know very little about pytesseract, but this might get you started:
#!/usr/bin/env python3
import numpy as np
from PIL import ImageGrab
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
from textblob import TextBlob
# Grab some screen
screen = ImageGrab.grab(bbox=(0,0,800,640))
# Make greyscale
w = screen.convert('L')
# Save so we can see what we grabbed
w.save('grabbed.png')
text = pytesseract.image_to_string(w)
correctedText = TextBlob(text).correct()
print(correctedText)
From this grab:
I got:
# Terminal Shell Edit View Window Help
The writing is on the wall
Related
I am trying to write a screen recorder program in python. My code runs normally in the compiler. But when I convert it to .exe, it raises this error:
[ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (415) cv::VideoWriter::open VIDEOIO(CV_IMAGES): raised OpenCV exception:OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): project.avi in function 'cv::icvExtractPattern'
I used pyinstaller to convert to .exe.
This is my code:
from tkinter import*
from tkinter import messagebox as msj
from PIL import ImageTk, Image
from PIL import ImageGrab
import os
import time
import cv2
import numpy as np
import glob
recording=False
i = 0
size = 100, 100
mainWindow=Tk()
mainWindow.title("ScreenRecorder")
mainWindow.geometry("200x200")
scriptDirectory = (os.path.dirname(os.path.realpath(__file__)))
def convert(imageCount):
img_array = []
for ip in range(1,imageCount):
x="snap"+str(ip)+".jpg"
for filename in glob.glob(x):
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 9, size)
for iz in range(len(img_array)):
out.write(img_array[iz])
out.release()
for a in range(1,imageCount+1):
os.remove("snap"+str(a)+".jpg")
def record():
global i
print(recording)
if(recording==True):
i+=1
fileName= ("snap"+str(i))
#time.sleep(0.00005)
image = ImageGrab.grab()
name=fileName+".jpg"
image.save(name,'JPEG')
imgX = (Image.open("snap"+str(i)+".jpg"))
imgX= imgX.resize(size, Image.ANTIALIAS)
imgX=ImageTk.PhotoImage(imgX)
mainWindow.after(1, record)
def startButton():
global recording
print("ehe")
recording=True
record()
def stopButton():
global recording
recording=False
record()
convert(i)
startButton=Button(text="Start",command=startButton)
startButton.pack()
stopButton=Button(text="Stop",command=stopButton)
stopButton.pack()
mainWindow.after(1, record)
mainWindow.mainloop()
I can just advise you to use another method, i think it's more simple, try to use 'auto py to exe'.This is a module that can be installed from the net or from the pip installer.
see from here.This the only way that i use for my codes.
Secondly, iknow that if the program is not opened using the format of .py will never be opened at .exe
hope i helped you.
If we look at the previews of the environments, they show the episodes increasing in the animation on the bottom right corner. https://gym.openai.com/envs/CartPole-v1/ .Is there a command to explicitly show that?
I don't think there is a command to do that directly available in OpenAI, but I've written some code that you can probably adapt to your purposes. This is the end result:
These is how I achieve the end result:
For each step, you obtain the frame with env.render(mode='rgb_array')
You convert the frame (which is a numpy array) into a PIL image
You write the episode name on top of the PIL image using utilities from PIL.ImageDraw (see the function _label_with_episode_number in the code snippet).
You save the labeled image into a list of frames.
You render the list of frames as a GIF using matplotlib utilities.
Here is the code I wrote for obtaining a GIF of the behavior of a random agent with the Episode number displayed in the top left corner of each frame:
import os
import imageio
import numpy as np
from PIL import Image
import PIL.ImageDraw as ImageDraw
import matplotlib.pyplot as plt
def _label_with_episode_number(frame, episode_num):
im = Image.fromarray(frame)
drawer = ImageDraw.Draw(im)
if np.mean(im) < 128:
text_color = (255,255,255)
else:
text_color = (0,0,0)
drawer.text((im.size[0]/20,im.size[1]/18), f'Episode: {episode_num+1}', fill=text_color)
return im
def save_random_agent_gif(env):
frames = []
for i in range(5):
state = env.reset()
for t in range(500):
action = env.action_space.sample()
frame = env.render(mode='rgb_array')
frames.append(_label_with_episode_number(frame, episode_num=i))
state, _, done, _ = env.step(action)
if done:
break
env.close()
imageio.mimwrite(os.path.join('./videos/', 'random_agent.gif'), frames, fps=60)
env = gym.make('CartPole-v1')
save_random_agent_gif(env)
You can find a working version of the code here: https://github.com/RishabhMalviya/dqn_experiments/blob/master/train_and_visualize.py#L10
I've been tried control PTZ camera pro 2, but the video stream and pan tilt still can't work together. Maybe someone can solve this issue. Feedback and suggestion are welcome.
This is complete code using python language for control PTZ Camera.
https://github.com/deviraanggi/Logitech-PTZ-Pro-2-Control
python3
from ctypes import *
import time
import ctypes
import clr
import os
import sys
import cv2
import PTZ
import clr
clr.AddReference('cobabikindll')
import PTZ
from PTZ import PTZDevicess, PTZType
device = PTZDevicess.GetDevice("PTZ Pro 2", PTZType.Absolute);
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)
#device.Move(-1, 1)
device.MoveInternal(30,35)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
"Why is plt.imshow (image) only getting output when written in last? How do I show output for all written plt.imshow (image)?I am unable to get all the output where i have used this plt.imshow(image)"
import os.path
from skimage.io import imread
from skimage import data_dir
import numpy as np
img=imread(os.path.join(data_dir,'astronaut.png'))
plt.imshow(img)
#1. image slicing from the original image
img_slice=img.copy()
img_slice=img_slice[0:300,360:480]
plt.imshow(img_slice)
img_slice[np.greater_equal(img_slice[:,:,0],100) & np.less_equal(img_slice[:,:,0],150)]=0
plt.imshow(img_slice)
# Fix the new rocket image back to its place in the original image.
img[0:300,360:480,:]=img_slice
plt.imshow(img)
"I am only getting the image for the last written plt.imshow(img). I am unable to get all the images as the output for the written plt.imshow()"
Just add plt.show() after your plt.imshow() calls!
Also, try to provide the exact same code you are using. Your code snippet doesn't have imports for plt.
Anyways, For reference,
import os.path
from skimage.io import imread
from skimage import data_dir
import numpy as np
import matplotlib.pyplot as plt
data_dir = '/some/directory/'
img=imread(os.path.join(data_dir,'astronaut.png'))
plt.imshow(img)
plt.show()
#1. image slicing from the original image
img_slice=img.copy()
img_slice=img_slice[0:300,360:480]
plt.imshow(img_slice)
plt.show()
img_slice[np.greater_equal(img_slice[:,:,0],100) & np.less_equal(img_slice[:,:,0],150)]=0
plt.imshow(img_slice)
plt.show()
# Fix the new rocket image back to its place in the original image.
img[0:300,360:480,:]=img_slice
plt.imshow(img)
plt.show()
I wanted to ask some help regarding tkinter, in python3.
I can't seem to display a jpeg image file in a label using the following code:
def changephoto(self):
self.tmpimgpath = filedialog.askopenfilename(initialdir=os.getcwd())
self.installimagepath.set(self.tmpimgpath)
self.selectedpicture = PhotoImage(file=self.installimagepath.get())
self.PictureLabel.configure(image=self.selectedpicture)
It can do png Images just fine, but when I try to load a jpg image, all I i am able to get is the following error:
_tkinter.TclError: couldn't recognize data in image file
I went through all similar questions I could find, but they all seem to answer the same thing: "from PIL import ImageTk, Image"
When I try that ( for the moment, I am trying to use pillow, btw ), ImageTk doesn't seem to be available.
Any help would be greatly appreciated.
You have to install PIL: pip install pillow.
If pip does not successfully install pillow, you might have to try pip3 or pip3.7 (use bash to see which options you have)
You can open your image with ImageTk:
import os
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk
def changephoto():
root = tk.Tk()
PictureLabel= tk.Label(root)
PictureLabel.pack()
tmpimgpath = filedialog.askopenfilename(initialdir=os.getcwd())
selectedpicture= ImageTk.PhotoImage(file=tmpimgpath)
PictureLabel.configure(image=selectedpicture)
Solution provided by Chuck G worked. I can't tell why I initially couldn't import ImageTk, but that ended up to just work.
from PIL import ImageTk
This error could possibly happen because of relative file path or non-English characters in filepath ,So i made this function which works very good in windows and with any kinds of file paths :
def loadrelimages(relativepath):
from PIL import ImageTk, Image
import os
directory_path = os.path.dirname(__file__)
file_path = os.path.join(directory_path, relativepath)
img = ImageTk.PhotoImage(Image.open(file_path.replace('\\',"/")))
return img
for example load photo_2021-08-16_18-44-28.jpg which is at the same directory with this code:
from tkinter import *
import os
def loadrelimages(relativepath):
from PIL import ImageTk, Image
import os
directory_path = os.path.dirname(__file__)
file_path = os.path.join(directory_path, relativepath)
img = ImageTk.PhotoImage(Image.open(file_path.replace('\\',"/")))
return img
root = Tk()
canvas = Canvas(root, width=500, height=500)
canvas.pack()
loadedimage=loadrelimages('photo_2021-08-16_18-44-28.jpg')
canvas.create_image(250, 250, image=loadedimage)
root.mainloop()
try this!!!!