forked from discordtics/token-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
168 lines (134 loc) · 5.59 KB
/
model.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from .utils import cache_forever
import imagehash, base64
from PIL import Image
import numpy as np
from io import BytesIO
class Task:
def __init__(self, task_obj, challenge=None):
self.challenge = challenge
self.key = task_obj["task_key"]
self.url = task_obj["datapoint_uri"]
def _request(self, url, method="GET", http_client=None):
http_client = http_client if http_client is not None \
else self.challenge.http_client
return http_client.request(
method,
url,
headers={"Accept-Encoding": "gzip, deflate, br"}
)
#@cache_forever()
def content(self, **kw) -> bytes:
resp = self._request(self.url, **kw)
return resp.content
#@cache_forever()
def image(self, raw=False, encoded=False, **kw) -> Image.Image:
content = self.content(**kw)
if raw: return content
if encoded: return base64.b64encode(content)
return Image.open(BytesIO(content))
#@cache_forever()
def phash(self, size=16, **kw) -> str:
image = self.image(**kw)
phash = str(imagehash.phash(image, size))
self._phash = phash
return phash
# Additional Imports
# Additional Utility Class
class ImageProcessor:
def __init__(self):
pass
def grayscale(self, image):
return image.convert("L")
def rotate(self, image, angle):
return image.rotate(angle)
def apply_filter(self, image, kernel):
# Apply a convolutional filter to the image
img_array = np.array(image)
filtered_image = np.convolve(img_array, kernel, mode='same')
return Image.fromarray(filtered_image)
def resize(self, image, size):
return image.resize(size)
def crop(self, image, box):
return image.crop(box)
# Additional Function in the Task Class
def histogram(self, **kw) -> np.ndarray:
image = self.image(**kw)
histogram = np.histogram(np.array(image).ravel(), bins=256, range=(0, 256))
return histogram[0]
def apply_custom_filter(self, kernel, **kw) -> Image.Image:
image = self.image(**kw)
return ImageProcessor().apply_filter(image, kernel)
def rotate_and_resize(self, angle, size, **kw) -> Image.Image:
image = self.image(**kw)
rotated = ImageProcessor().rotate(image, angle)
resized = ImageProcessor().resize(rotated, size)
return resized
def crop_and_grayscale(self, box, **kw) -> Image.Image:
image = self.image(**kw)
cropped = ImageProcessor().crop(image, box)
grayscale = ImageProcessor().grayscale(cropped)
return grayscale
# Usage Example
if __name__ == "__main__":
# Create a Task instance
task_info = {
"task_key": "12345",
"datapoint_uri": "https://example.com/image.jpg"
}
task = Task(task_info)
# Perform image processing operations
histogram = task.histogram()
custom_filter = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
filtered_image = task.apply_custom_filter(custom_filter)
rotated_resized_image = task.rotate_and_resize(angle=30, size=(200, 200))
cropped_grayscale_image = task.crop_and_grayscale(box=(50, 50, 150, 150))
# Print results
print("Image Histogram:", histogram)
filtered_image.show()
rotated_resized_image.show()
cropped_grayscale_image.show()
# Additional Utility Class for Complex Image Processing
class AdvancedImageProcessor:
def __init__(self):
pass
def apply_edge_detection(self, image):
# Apply edge detection algorithm to the image
# You can use popular edge detection algorithms like Canny or Sobel here.
# For demonstration, we use a simple edge detection filter.
edge_filter = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
return ImageProcessor().apply_filter(image, edge_filter)
def apply_advanced_filter(self, image):
# Apply a more advanced image filter
# You can implement custom filters or use advanced filtering techniques here.
# For demonstration, we use a sharpening filter.
sharpening_filter = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
return ImageProcessor().apply_filter(image, sharpening_filter)
def apply_color_inversion(self, image):
# Invert colors in the image
return ImageOps.invert(image)
# Additional Function in the Task Class
def apply_edge_detection_filter(self, **kw) -> Image.Image:
image = self.image(**kw)
return AdvancedImageProcessor().apply_edge_detection(image)
def apply_advanced_image_filter(self, **kw) -> Image.Image:
image = self.image(**kw)
return AdvancedImageProcessor().apply_advanced_filter(image)
def invert_image_colors(self, **kw) -> Image.Image:
image = self.image(**kw)
return AdvancedImageProcessor().apply_color_inversion(image)
# Usage Example
if __name__ == "__main__":
# Create a Task instance
task_info = {
"task_key": "12345",
"datapoint_uri": "https://example.com/image.jpg"
}
task = Task(task_info)
# Perform advanced image processing operations
edge_detected_image = task.apply_edge_detection_filter()
advanced_filtered_image = task.apply_advanced_image_filter()
inverted_colors_image = task.invert_image_colors()
# Print results
edge_detected_image.show()
advanced_filtered_image.show()
inverted_colors_image.show()