I need help to make the pytesseract could select or choose folder or location to save the outputt result.
This is the code that I use
from tkinter import filedialog
import pytesseract as tess
from PIL import Image
tess.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
def extract():
files = filedialog.askopenfilenames(title="Select Images")
text = []
n = len(files)
for i in range(0,n):
img = Image.open(files[i])
text.append(tess.image_to_pdf_or_hocr(img, lang='nld', config='--psm 11 --oem 3'))
text_files = open(f'test{i+1}.pdf',"+wb")
text_files.write(text[i])
if __name__ == "__main__":
extract()
I don't have python background but eager to learn more.
Thanks
I have try few code but still not working.
Even better if someone could write and add the code to make it works
Related
I need help with my code. I have built a recommendation system using cosine similarity on a colab and used pickle to serialized it. when I deserialized it inside a colab file, it works perfectly fine but when I deserialize it in a new colab file. it gives me an error
name 'data' is not defined
data is a variable that is initialized with my dataset which is outside of the class InstaPost.
import pandas as pd
import numpy as np
from sklearn.feature_extraction import text
from sklearn.metrics.pairwise import cosine_similarity
import dill as pickle
data = pd.read_csv("/content/instaData.txt")
data
data = data[["Caption", "Hashtags"]]
captions = data["Caption"].tolist()
uni_tfidf = text.TfidfVectorizer(input=captions, stop_words="english")
uni_matrix = uni_tfidf.fit_transform(captions)
uni_sim = cosine_similarity(uni_matrix)
def recommend_post(x):
return ", ".join(data["Caption"].loc[x.argsort()[-7:-1]])
data["Recommended Post"] = [recommend_post(x) for x in uni_sim]
class InstaPost:
def Post(number):
count = 0
wordy = (data["Recommended Post"][number])
sentence = wordy.split(',')
for i in sentence:
count=count+1
print(count," ",i)
obj = InstaPost
obj.Post(1)
pickle_out = open("modelREC", "wb")
pickle.dump(obj, pickle_out)
pickle_out.close()
pickle_in = open("modelREC", "rb")
exe = pickle.load(pickle_in)
print(exe.Post(10))
NOTE: on a different file
print(exe.Post)
works
and give output
<function InstaPost.Post at 0x7efc0b4c3f70>
if I need to give the reference of the data than please guide me how should I do it. It will be a great help to me
Thanks in advance
I'm very new to coding and was coping this code from a youtube video I saw subbing in my own files for images when necessary. I got to the final step of testing the ai on new images and getting a result but this error message pops up.
dir_path = "/content/drive/MyDrive/darth_vader_Bing_images/test 1"
for i in os.listdir(dir_path ):
image = img.load_img(dir_path+'//'+ i)
plt.imshow(img)
plt.show
Sample code
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('cat1.jpeg')
plt.imshow(img)
What I want to do
I am trying to make an interactive plot for a Jupyter Notebook. The functions are all written in different files, but their intended use is in interactive notebook sessions. I have a Button widget on a matplotlib figure, which, when clicked, I want to open a file dialog where a user can enter a filename to save the figure to. I am on Mac OSX (Mojave 10.14.6) and Tkinter is giving me major problems (complete system crashes), so I am trying to implement this with PyQt5.
The code
-----------
plotting.py
-----------
from . import file_dialog as fdo
import matplotlib.pyplot as plt
import matplotlib.widgets as wdgts
def plot_stack(stack):
fig, ax = plt.subplots(figsize=(8, 6))
plt.subplots_adjust(bottom=0.25, left=-0.1)
... # plotting happens here
# button for saving
def dosaveframe(event):
fname = fdo.save()
fig.savefig(fname) # to be changed to something more appropriate
savea = plt.axes([0.65, 0.8, 0.15, 0.05], facecolor=axcolor)
saveb = Button(savea, "save frame", hovercolor="yellow")
saveb.on_clicked(dosaveframe)
savea._button = saveb # for persistence
plt.show()
--------------
file_dialog.py
--------------
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import (QWidget, QFileDialog)
class SaveFileDialog(QWidget):
def __init__(self, text="Save file", types="All Files (*)"):
super().__init__()
self.title = text
self.setWindowTitle(self.title)
self.types = types
self.filename = self.saveFileDialog()
self.show()
def saveFileDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
filename, _ = (
QFileDialog.getSaveFileName(self, "Enter filename",
self.types, options=options))
return filename
def save(directory='./', filters="All files (*)"):
"""Open a save file dialog"""
app = QApplication([directory])
ex = SaveFileDialog(types=filters)
return ex.filename
sys.exit(app.exec_())
What is not working
The save dialog opens and it responds to the mouse, but not to the keyboard. The keyboard stays connected to the notebook no matter if I select the little window, so when I press "s" it saves the notebook. As such, the user can not enter a file path. How can I make this work? I have Anaconda, PyQt 5.9.2, matplotlib 3.1.1, jupyter 1.0.0.
I found a very crappy, non-clean solution but it seems to work. For some reason, opening a QFileDialog directly does not allow me to activate it. It opens up behind the active window from where it was called (terminal window or browser in Jupyter Notebook) and does not respond to the keyboard. So the save function in the following block does NOT work as expected on Mac:
from PyQt5.QtWidgets import QApplication, QFileDialog
def save(directory='./', filters="All files (*)"):
app = QApplication([directory])
path, _ = QFileDialog.getSaveFileName(caption="Save to file",
filter=filters,
options=options)
return path
What does work is if the file dialog is opened from a widget. So working with a dummy widget that never shows up on the screen does work for me, at least from the command line:
from PyQt5.QtWidgets import (QApplication, QFileDialog, QWidget)
class DummySaveFileDialogWidget(QWidget):
def __init__(self, title="Save file", filters="All Files (*)"):
super().__init__()
self.title = title
self.filters = filters
self.fname = self.savefiledialog()
def savefiledialog(self):
filename, _ = QFileDialog.getSaveFileName(caption=self.title,
filter=self.filters,
options=options)
return filename
def save(directory='./', filters="All files (*)"):
app = QApplication([directory])
form = DummySaveFileDialogWidget()
return form.fname
If anyone finds a more elegant solution that works let me know
EDIT: this works when it is called from the command line, but still not from a Jupyter Notebook. Also tried this, no success. The file dialog stays behind the browser window and does not respond to the keyboard.
I found out from here that I can create and save animated GIFs using Pillow. However, it doesn't look like the save method returns any value.
I can save the GIF to a file and then open that file using Image.open, but that seems unnecessary, given that I don't really want the GIF to be saved.
How can I save the GIF to a variable, rather than a file?
That is, I would like to be able to do some_variable.show() and display a GIF, without ever having to save the GIF onto my computer.
To avoid writing any files, you can just save your image to BytesIO object. For example:
#!/usr/bin/env python
from __future__ import division
from PIL import Image
from PIL import ImageDraw
from io import BytesIO
N = 25 # number of frames
# Create individual frames
frames = []
for n in range(N):
frame = Image.new("RGB", (200, 150), (25, 25, 255*(N-n)//N))
draw = ImageDraw.Draw(frame)
x, y = frame.size[0]*n/N, frame.size[1]*n/N
draw.ellipse((x, y, x+40, y+40), 'yellow')
# Saving/opening is needed for better compression and quality
fobj = BytesIO()
frame.save(fobj, 'GIF')
frame = Image.open(fobj)
frames.append(frame)
# Save the frames as animated GIF to BytesIO
animated_gif = BytesIO()
frames[0].save(animated_gif,
format='GIF',
save_all=True,
append_images=frames[1:], # Pillow >= 3.4.0
delay=0.1,
loop=0)
animated_gif.seek(0,2)
print ('GIF image size = ', animated_gif.tell())
# Optional: display image
#animated_gif.seek(0)
#ani = Image.open(animated_gif)
#ani.show()
# Optional: write contents to file
animated_gif.seek(0)
open('animated.gif', 'wb').write(animated_gif.read())
In the end, variable animated_gif contains contents of the following image:
However, displaying an animated GIF in Python is not very reliable. ani.show() from the code above displays only first frame on my machine.
Could someone please tell me whether Tensorboard supports exporting CSV files from the command line? The reason why I ask this is because I have a lots of logging directory and I am hoping to have a script file that automates the process. Thanks.
The API supports reading files programmatically. Here's an example of extracting data for a tag and saving it to a .csv in a similar format to those generated by tensorboard
import argparse
import numpy as np
import tensorflow as tf
def save_tag_to_csv(fn, tag='test_metric', output_fn=None):
if output_fn is None:
output_fn = '{}.csv'.format(tag.replace('/', '_'))
print("Will save to {}".format(output_fn))
sess = tf.InteractiveSession()
wall_step_values = []
with sess.as_default():
for e in tf.train.summary_iterator(fn):
for v in e.summary.value:
if v.tag == tag:
wall_step_values.append((e.wall_time, e.step, v.simple_value))
np.savetxt(output_fn, wall_step_values, delimiter=',', fmt='%10.5f')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('fn')
parser.add_argument('--tag', default='test_metric')
args = parser.parse_args()
save_tag_to_csv(args.fn, tag=args.tag)