-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadImage.py
97 lines (71 loc) · 2.94 KB
/
readImage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from PIL import Image
import pytesseract
import numpy as np
import matplotlib.pyplot as plt
import json
import matplotlib.patches as patches
def draw_on_image():
# img = plt.imread("screenshot_0.jpg")
# fig, ax = plt.subplots()
# with open("screenshot_0.json") as f:
# screenshot_data = json.load(f)
# print(screenshot_data)
# ax.imshow(img)
# x = screenshot_data["gaze_x"]
# y = screenshot_data["gaze_y"]
# ax.scatter(x, y)
# plt.savefig("screenshot_with_draw_0")
# # plt.show()
# gpt code
img = plt.imread("screenshot_0.jpg")
fig, ax = plt.subplots()
with open("screenshot_0.json") as f:
screenshot_data = json.load(f)
ax.imshow(img)
x = screenshot_data["gaze_x"]
y = screenshot_data["gaze_y"]
# Assuming each point in x and y is a center of a square
for xi, yi in zip(x, y):
# Create a square as a rectangle, adjust the conversion from pixels to data coordinates as needed
square = patches.Rectangle(
(xi - 150, yi - 150), 300, 300, linewidth=1, edgecolor="r", facecolor="none"
)
ax.add_patch(square)
plt.savefig("screenshot_with_draw_0")
# plt.show()
def read_image():
with open("screenshot_0.json") as f:
screenshot_data = json.load(f)
print(screenshot_data)
screenshot = Image.open("screenshot_0.jpg")
# write code to draw a box at the different points, this is where you're going to OCR.
# define width and height of this box
crop_width, crop_height = 300, 300
timesteps = screenshot_data["time"]
# at each time step, crop the image at the x,y coordinates and save
for timestep in timesteps:
image_x = screenshot_data["gaze_x"][timestep]
image_y = screenshot_data["gaze_y"][timestep]
# Calculate the top-left corner of the cropping box
start_x = image_x - crop_width // 2
start_y = image_y - crop_height // 2
# Crop the image
cropped_image = screenshot.crop(
(start_x, start_y, start_x + crop_width, start_y + crop_height)
)
# save image - change this to save it into a folder
cropped_image.save(f"cropped_images/cropped_image_{timestep}.jpg")
crop_text = pytesseract.image_to_string(cropped_image)
print("timestep:", timestep)
print("crop_text:", crop_text)
if __name__ == "__main__":
pytesseract.pytesseract.tesseract_cmd = (
# r"c:\Program Files\Tesseract-OCR\tesseract.exe" # Nyan's one
r"c:\Users\allis\AppData\Local\Programs\Tesseract-OCR\tesseract.exe"
)
draw_on_image()
# look at the screenshot_with_draw image we've created, we need a way to intnelligently stitch
# these bounded boxes together and pass them through the OCR instead, of just individual screenshots.
# you have to also somehow do this with time as well, as you want the text to have an order, but first
# just start off overall.
# read_image()