-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotation.py
75 lines (69 loc) · 2.45 KB
/
rotation.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
import requests
import json
import io
import cv2
import imutils
from PIL import Image
k=0
angle=0
API_KEY='API_KEY'
############################### tester les rotation avec angle 330 -> 360 avec un pas 5 degree########
for i in range (330,360,5):
img = cv2.imread('exemple3.jpg')
img=imutils.rotate(img,angle=i)
cv2.imwrite('rotated.jpg', img)
height, width, _ = img.shape
roi = img[0: height, 0: width]
url_api = "https://api.ocr.space/parse/image"
_, compressedimage = cv2.imencode(".jpg", roi, [1, 90])
file_bytes = io.BytesIO(compressedimage)
result = requests.post(url_api,
files = {'pic.jpg': file_bytes},
data = {'apikey': API_KEY,
'language': "ara"})
result = result.content.decode()
result = json.loads(result)
#print(result)
if (len(result.get("ParsedResults"))!=0):
parsed_results = result.get("ParsedResults")[0]
text_detected = parsed_results.get("ParsedText")
#word_list = text_detected.split()
#number_of_words = len(word_list)
#print(number_of_words)
print(len(text_detected))
if (k<len(text_detected)):
k=len(text_detected)
angle=i
with open ('space.txt','w',encoding='utf-8')as myfile:
myfile.write(text_detected)
############################### tester les rotations avec angle 0 -> 30 avec un pas 5 degree########
for i in range (0,30,5):
img = cv2.imread('exemple3.jpg')
img=imutils.rotate(img,angle=i)
cv2.imwrite('rotated_30.jpg', img)
height, width, _ = img.shape
roi = img[0: height, 0: width]
url_api = "https://api.ocr.space/parse/image"
_, compressedimage = cv2.imencode(".jpg", roi, [1, 90])
file_bytes = io.BytesIO(compressedimage)
result = requests.post(url_api,
files = {'pic.jpg': file_bytes},
data = {'apikey': API_KEY,
'language': "ara"})
result = result.content.decode()
result = json.loads(result)
if (len(result.get("ParsedResults"))!=0):
parsed_results = result.get("ParsedResults")[0]
text_detected = parsed_results.get("ParsedText")
#word_list = text_detected.split()
#number_of_words = len(word_list)
#print(number_of_words)
print(len(text_detected))
if (k<len(text_detected)):
k=len(text_detected)
angle=i
with open ('space.txt','w',encoding='utf-8')as myfile:
myfile.write(text_detected)
img = cv2.imread('exemple3.jpg')
img=imutils.rotate(img,angle=angle)
cv2.imwrite('final_pic.jpg', img)