forked from Wissididom/SafeGIF-Twitch-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
71 lines (52 loc) · 2.04 KB
/
api.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
import cv2
import numpy
import requests
import urllib.parse
from io import BytesIO
from PIL import Image
# These values are not backed by resources just random guesses (trial and error)
# Defines the minimum amount of average luminosity change to the previous frame
# for a frame to be considered a flash.
FLASH_THRESHOLD = 25
# The maximum amount of flashing frames per second for a gif not to be rejected.
# For example a gif with a looping duration of 500ms and 2 flashes will result in
# a value of 4 flashes per second.
MAX_FLASHES_PER_SECOND = 1.5
def is_url(string):
# Parse the string as a URL
parsed = urllib.parse.urlparse(string)
# Check if the scheme is present
return bool(parsed.scheme)
def get_luminance_diff(luminance_frame, luminance_prev_frame):
bgr = cv2.cvtColor(luminance_frame, cv2.COLOR_RGB2BGR)
luminances = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
prev_bgr = cv2.cvtColor(luminance_prev_frame, cv2.COLOR_RGB2BGR)
prev_luminances = cv2.cvtColor(prev_bgr, cv2.COLOR_BGR2GRAY)
diff = cv2.subtract(luminances, prev_luminances)
return diff
def process_gif(gif_path):
if is_url(gif_path):
response = requests.get(gif_path)
img = Image.open(BytesIO(response.content))
else:
img = Image.open(gif_path)
if not img.is_animated:
print("Image is not animated.")
return False
duration = 0
num_flashes = 0
prev_frame = None
for i in range(0, img.n_frames):
img.seek(i)
duration += img.info['duration']
frame = numpy.array(img.convert('RGB'))
# If not the first frame calculate diff to previous frame
# If too much average diff in luminance the frame transition is considered a flash
if i != 0:
luminance_diff = get_luminance_diff(frame, prev_frame)
average_diff = numpy.average(luminance_diff)
if average_diff >= FLASH_THRESHOLD:
num_flashes += 1
prev_frame = frame
img.close()
return num_flashes * (1000 / duration) >= MAX_FLASHES_PER_SECOND