I'm a beginner in this field and trying to make a gradio project of detecting similar image.
Here's my code -
import gradio as gr
from DeepImageSearch import Index, LoadData,SearchImage
import os;
def similar(input_img):
image_list = LoadData().from_folder(["C:/Users/HP/Pictures/abc"])
Index(image_list).Start()
img=SearchImage().get_similar_images(image_path=os.path.abspath(input_img))
return img
demo = gr.Interface(similar, gr.Image(shape=(200, 200)), "image")
demo.launch()
error I am getting
Related
I am trying to write a program that reads in a folder of photos, analyses their heights and widths, and resizes them accordingly, then sends them to a word document. I keep getting this error and I am unsure what is causing it:
from docx import Document
import cv2
from PIL import Image
import glob
import os
import numpy
document = Document()
img_dir = "C:/Users/27832/Desktop/Report Images"
data_path = os.path.join(img_dir,'*g')
files = glob.glob(data_path)
photos = []
for pic in files:
imagg = cv2.imread(pic)
photos.append(imagg)
for i in range(0, len(photos)):
if 0.85*(photos[i].shape[0]) < (photos[i].shape[1]) < 1.15*(photos[i].shape[0]):
resized_image = photos[i].resize((314, 314))
document.add_picture(resized_image)
elif (photos[i].shape[1]) >= 0.85*(photos[i].shape[0]):
resized_image = photos[i].resize((257, 382))
document.add_picture(resized_image)
elif (photos[i].shape[1]) <= 1.15*(photos[i].shape[0]):
resized_image = photos[i].resize((401, 325))
document.add_picture(resized_image)
document.save("C:/Users/27832/Desktop/Word Macro Program/Report.docx")
I am taking a screenshot, and then I need to reference the shot I just took so I can translate what's inside it.
When I directly pass a file location, e.g "filex.png", to readtext, it works, but I just need it to pass the written file into it.
import easyocr
import pyautogui
import time
import cv2
import numpy as np
reader = easyocr.Reader(['en'])
tag = 1
for i in range(2):
time.sleep(4)
image = pyautogui.screenshot(region=(630,400,650,130))
image = cv2.cvtColor(np.array(image),
cv2.COLOR_RGB2BGR)
tag+=1
img = cv2.imwrite(f"image{tag}.png", image)
results = reader.readtext(img)
text=""
for result in results:
text += result[1] + " "
print(text)
In answer to your specific question, I think you're looking for something like:
import easyocr
import pyautogui
import time
import cv2
import numpy as np
reader = easyocr.Reader(['en'])
tag = 1
for i in range(2):
time.sleep(4)
image = pyautogui.screenshot(region=(630, 400, 650, 130))
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
tag += 1
f_name = f"image{tag}.png"
cv2.imwrite(f_name, image)
results = reader.readtext(f_name)
text = ""
for result in results:
text += result[1] + " "
print(text)
You can just store your file name in a variable in pass it to both imwrite and readtext
There are other options as well, depending on what information you need access to within the program, and how quickly you need to process your data.
Option: Pass the np.array directly to readtext
image = pyautogui.screenshot(region=(630, 400, 650, 130))
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
results = reader.readtext(image)
Option: Pass the data from the written file to the readtext function.
f_name = f"image{tag}.png"
cv2.imwrite(f_name, image)
with open(f_name, 'rb') as f:
results = reader.readtext(f.read())
import pyautogui
import easyocr
import numpy as np
reader = easyocr.Reader(['en'],gpu=False)
im = pyautogui.screenshot(region=(630,400,650,130)
result = reader.readtext(np.array(im),detail = 0)
just pass the pyautogui image as np.array
I have been attempting to produce an OCR tool following this tutorial on youtube, and using the following script:
import os
import sys
import cv2
import numpy as np
input_f = 'letter.data'
img_resize_factor = 12
start, end = 6, -1
height, width = 16, 8
with open(input_f, 'r') as f:
for line in f.readlines():
data = np.array([255*float(x) for x in line.split('\t')[start:end]])
img = np.reshape(data, (height, width))
img_scaled = cv2.resize(img, None, fx=img_resize_factor, fy=img_resize_factor)
print(line)
cv2.imshow('img', img_scaled)
c = cv2.waitKey()
if c == 27:
break
The code falls over when attempting to use cv2.imshow('img', img_scaled) the window appears however is non responding and the image is not loaded into it.
I am using the most up to date version of OpenCV, I am running this in VisualStudio, and have had to add "python.linting.pylintArgs": ["--extension-pkg-whitelist=cv2"] to the user settings.
The error I get is:
Exception has occurred: cv2.error OpenCV(4.0.0)
c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261:
error: (-2:Unspecified error) in function '__cdecl
cv::CvtHelper,struct cv::Set<3,4,-1>,struct
cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class
cv::_OutputArray &,int)' > Unsupported depth of input image: >
'VDepth::contains(depth)' > where > 'depth' is 6 (CV_64F) File
"C:\Users\aofarrell\Desktop\Python\NeuralNetworks\SimpleOCR.py", line
23, in
break
Everything in your script is wrong.
Solution
1) If you are opening a file, open just file, get data and get out of with statement
2) The error you are experiencing is due to wrong shape
I opened the file and extracted images
import os
import sys
import cv2
import numpy as np
input_f = 'letter.data'
start, end = 6, -1
def file_opener(input_f):
with open(input_f,'r') as fl:
for line in fl.readlines():
yield np.array([255*float(x) for x in line.split('\t')[start:-1]])
iterator = file_opener(input_f)
images = np.array([row for row in iterator]).reshape(52152,16,8) # array with 52152 images
for image_index in range(images.shape[0]):
IMAGE = cv2.resize(images[image_index,:],(0,0),fx=5,fy=5)
cv2.imshow('image {}/{}'.format(image_index,images.shape[0]),IMAGE)
cv2.waitKey(0)
cv2.destroyAllWindows(0)
I am working on an art project and am converting text to binary to images.
So far, I have what I think is the hard part done and am almost there ...
except i'm not quite sure how to approach this last bit of the problem:
I have a series of images displayed one after another but I need to wrap the images so that they are formatted to all fit on a page of A4 (printer paper) in landscape orientation.
How should I approach this?
I have the basic script to convert text to binary to images.
One additional issue I'm seeing is that when I run the script on the entire passage, too many files are opened and I receive an error(shown below):
Traceback (most recent call last):
File "ascii_to_binary.py", line 73, in <module>
imgs = [Image.open(i) for i in list_im]
File "/home/odroid/.virtualenvs/cv/local/lib/python2.7/site-packages/PIL/Image.py", line 2410, in open
fp = builtins.open(filename, "rb")
IOError: [Errno 24] Too many open files: 'open_circle.png'
Here's the script in its entirety for reference:
import numpy as np
import cv2
import imutils
import sys
from PIL import Image
open_circle = 'open_circle.png'
closed_circle = 'closed_circle.png'
diamond = 'diamond.png'
text1 = open("filename.text", "r")
letters = text1.read()
a = len(letters)
binary_text = [None]*a #pre-allocating a list
encoded_bin = ' '.join([bin(ord(letter))[2:].zfill(8) for letter in letters])
b = len(encoded_bin[0:18])
list_im = [0]*b
for val in range(b):
if encoded_bin[val] == '0':
list_im[val] = closed_circle
if encoded_bin[val] == '1':
list_im[val] = open_circle
if encoded_bin[val] == ' ':
list_im[val] = diamond
imgs = [Image.open(i) for i in list_im]
min_shape = sorted( [(np.sum(i.size), i.size) for i in imgs] )[0][1]
imgs_comb = np.hstack( (np.asarray(i.resize(min_shape)) for i in imgs) )
imgs_comb = Image.fromarray(imgs_comb)
imgs_comb.show()
I am trying to access file_name dynamically from user and then pass it to videoCapture(file_name) and then process it.
Code :
import cv2
import numpy as np
import os
import sqlite3
import pickle
from PIL import Image
import sys
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
rec = cv2.createLBPHFaceRecognizer();
'''
Dynamically accessing the fileName
Error seems to be here in the following couple of codes
Note: i am assigning file_name as <"test.mp4">
'''
file_name = raw_input("Enter file name: ")
print file_name
cam = cv2.VideoCapture(file_name)
while cam.isOpened():
ret,img = cam.read()
if ret == True:
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces :
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
id,conf=rec.predict(gray[y:y+h,x:x+w])
'''
Few lines of code
'''
cv2.imshow("Face",img);
if (cv2.waitKey(1) == ord('q')):
break;
else :
print ('ret is false')
break
cam.release()
cv2.destroyAllWindows()
it show no error but it does not execute the while(cam.isOpened): loop. am i missing something ?
Enter the filename without the quotes. It works fine. Because since the input has alphabets it will be string object already. Adding quotes will be like inputting a wrong file name. As I said in comments videocapture does not throw error sometimes if entered filename does not exist. Hope this helps