-
Notifications
You must be signed in to change notification settings - Fork 0
/
testModules.py
615 lines (575 loc) · 19.9 KB
/
testModules.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# TEXT TO SPEECH
# import gtts
# from playsound import playsound
#
# tts = gtts.gTTS("Hello world")
# tts.save("hello.mp3")
# playsound("hello.mp3")
#
# # SCREEN SHARING
#
import pyautogui
# im2 = pyautogui.screenshot('my_screenshot2.png', region=(0,0,1920,1080))
# DETECTING MOUSE CLICKS
from pynput.mouse import Button, Controller
#
# mouse = Controller()
# Read pointer position
# print('The current pointer position is {0}'.format(
# mouse.position))
# Set pointer position
# mouse.position = (10, 20)
# print('Now we have moved it to {0}'.format(
# mouse.position))
# mouse.click(Button.left, 2)
from pynput.mouse import Listener
# def on_move(x, y):
# print('Pointer moved to {0}'.format(
# (x, y)))
#
#
# def on_click(x, y, button, pressed):
# print('{0} at {1}'.format(
# 'Pressed' if pressed else 'Released',
# (x, y)))
# # if not pressed:
# # # Stop listener
# # return False
#
#
# def on_scroll(x, y, dx, dy):
# print('Scrolled {0}'.format(
# (x, y)))
# Collect events until released
# with Listener(
# # on_move=on_move,
# on_click=on_click) as listener:
# # on_scroll=on_scroll)
# listener.join()
# OPTICAL CHARACTER RECOGNITION
# import cv2
# import pytesseract
#
# img = cv2.imread('image.jpg')
#
# # Adding custom options
# custom_config = r'--oem 3 --psm 6'
# print(pytesseract.image_to_string(img, config=custom_config))
# from pynput.mouse import Listener
#
# def on_click(x, y, button, pressed):
# print('{0} at {1}'.format(
# 'Pressed' if pressed else 'Released',
# (x, y)))
#
#
# with Listener(on_click=on_click) as listener:
# listener.join()
# yCoord = 0
# xCoord = 0
# counter = 0
# xLis = []
# yLis = []
# maxX = 0
# for text, top, left, width, height in zip(dict["text"], dict["top"],
# dict["left"], dict["width"],
# dict["height"]):
# if text in ["The", "cat", "(Felis", "catus)"]:
# if counter == 1:
# yCoord = top
# xCoord = left
# counter += 1
#
# if counter >= 1 and yCoord - 10 < top < yCoord + 10:
# # img = cv2.rectangle(img, (left, top), (left + width, top + height),
# # (0, 255, 0), 2)
# xLis.append(left)
# yLis.append(top)
# if left == max(xLis):
# maxX = left + width
# elif counter >= 1 and xCoord - 10 < left < maxX:
# # img = cv2.rectangle(img, (left, top), (left + width, top + height),
# # (0, 255, 0), 2)
# xLis.append(left)
# yLis.append(top)
# img = cv2.rectangle(img, (min(xLis), min(yLis)), (max(xLis), max(yLis)),
# (255, 0, 0), 2)
# # print(pytesseract.image_to_string(img, config=custom_config))
# cv2.imshow('img', img)
# cv2.waitKey(0)
# import cv2
# import pytesseract
# from pytesseract import Output
#
# img = cv2.imread('sampleImages/fullPage2.jpg', cv2.IMREAD_GRAYSCALE)
#
# # thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
#
# custom_config = r'--oem 3 --psm 1'
# string = pytesseract.image_to_string(img, config=custom_config)
#
# sectionsLst = string.split("\n\n")
# cleanSectionLst = []
#
# # create a new list of words separated by section
# for i in sectionsLst:
# cleanSectionLst.append(i.replace("\n", " ").split(" "))
#
# for i in range(len(cleanSectionLst)):
# if " " in cleanSectionLst[i]:
# cleanSectionLst[i] = list(filter(" ".__ne__, cleanSectionLst[i]))
# if "" in cleanSectionLst[i]:
# cleanSectionLst[i] = list(filter("".__ne__, cleanSectionLst[i]))
#
# if [] in cleanSectionLst:
# cleanSectionLst.remove([])
#
# # extra relevant data about the words on the image - coordinates
# dataDict = pytesseract.image_to_data(img, output_type=Output.DICT,
# config=custom_config)
# lstWords = list(dataDict["text"])
#
#
# def betterIdx(lst, word, num):
# # extracts the nth duplicate's index
# counter = 0
# lastIdx = 0
# for i in range(len(lst)):
# if lst[i] == word:
# counter += 1
# lastIdx = i
# if counter == num:
# return i
# return lastIdx
#
#
# # split each section with associated indices
# idxDict = {}
# dupWords = {}
#
# # links each section to the indices of the words in that section
# for i in cleanSectionLst:
# val = []
# for word in i:
# if word in lstWords:
# val.append(betterIdx(lstWords, word, dupWords.get(word, 1)))
# # to avoid issues with duplicates
# dupWords[word] = dupWords.get(word, 1) + 1
# idxDict[" ".join(i)] = val
#
# # extracts coordinates of each word in section
# coordDict = {}
# for key in idxDict:
# coordDict[key] = []
# for val in idxDict[key]:
# coordDict[key].append([dataDict["left"][val], dataDict["top"][val],
# dataDict["width"][val], dataDict["height"][val]])
#
# # find min and max coord of coordinates of words to get overall coordinates
# # for the whole section
#
# minMaxCoord = {}
#
# for elem in coordDict:
# minMaxCoord[elem] = [99999, 99999, 0, 0]
# for i in coordDict[elem]:
# # print(i[0], i[1], minMaxCoord[elem])
# if i[0] <= minMaxCoord[elem][0]:
# minMaxCoord[elem][0] = i[0]
#
# if i[0] + i[2] >= minMaxCoord[elem][2]:
# minMaxCoord[elem][2] = i[0] + i[2]
#
# if i[1] <= minMaxCoord[elem][1]:
# minMaxCoord[elem][1] = i[1]
#
# if i[1] + i[3] >= minMaxCoord[elem][3]:
# minMaxCoord[elem][3] = i[1] + i[3]
# NEW CHANGES START HERE
# smallSections = []
#
# for text in minMaxCoord:
# sectionSize = abs(minMaxCoord[text][2] - minMaxCoord[text][0]) * abs(minMaxCoord[text][3] - minMaxCoord[text][1])
# if sectionSize < 20000:
# smallSections.append(text)
#
# combinedTexts = []
# newMinMaxCoord = {}
#
#
# def combineSections(minMaxDict, text1, text2):
# combineText = text1 + " " + text2
# combineCoord = [min(minMaxDict[text1][0], minMaxDict[text2][0]), min(minMaxDict[text1][1], minMaxDict[text2][1]),
# max(minMaxDict[text1][2], minMaxDict[text2][2]), max(minMaxDict[text1][3], minMaxDict[text2][3])]
# return combineText, combineCoord
#
#
# for text in smallSections:
# topX = minMaxCoord[text][0]
# topY = minMaxCoord[text][1]
# botX = minMaxCoord[text][2]
# botY = minMaxCoord[text][3]
# marginY = 20
# marginX = 40
# if text not in combinedTexts:
# remainList = smallSections[:smallSections.index(text)] + smallSections[smallSections.index(text) + 1:]
# for elem in remainList:
# if elem not in combinedTexts:
# if (topY - marginY < minMaxCoord[elem][3] < topY or botY < minMaxCoord[elem][1] < botY + marginY) and \
# (topX - marginX < minMaxCoord[elem][0] < topX or botX < minMaxCoord[elem][2] < botX + marginX):
# newText, newCoord = combineSections(minMaxCoord, text, elem)
# combinedTexts += [text, elem]
# newMinMaxCoord[newText] = newCoord
#
# for i in minMaxCoord:
# if i not in smallSections or (i in smallSections and i not in combinedTexts):
# newMinMaxCoord[i] = [minMaxCoord[i][0], minMaxCoord[i][1], minMaxCoord[i][2], minMaxCoord[i][3]]
#
# for i in newMinMaxCoord:
# img = cv2.rectangle(img, (newMinMaxCoord[i][0], newMinMaxCoord[i][1]),
# (newMinMaxCoord[i][2], newMinMaxCoord[i][3]), (0, 0, 0), 2)
# for i in minMaxCoord:
# img = cv2.rectangle(img, (minMaxCoord[i][0], minMaxCoord[i][1]),
# (minMaxCoord[i][2], minMaxCoord[i][3]), (0, 0, 0), 2)
# cv2.imshow('img', img)
# cv2.waitKey(0)
# check if two screenshots are the same - (abhi, StackOverFlow, 2020)
# https://stackoverflow.com/questions/1927660/compare-two-images-the-python-linux-way
# with open('sampleImages/test.jpg', 'rb') as f:
# content1 = f.read()
# with open('sampleImages/test2.jpg', 'rb') as f:
# content2 = f.read()
# if content1 == content2:
# print("same")
# else:
# print("not same")
# from pynput import keyboard
#
#
# def on_press(key):
# print(key)
#
#
# with keyboard.Listener(
# on_press=on_press) as listener:
# listener.join()
# import cv2
# import pytesseract
# from pytesseract import Output
#
# img = cv2.imread('sampleImages/ArPageTest.jpg', cv2.IMREAD_GRAYSCALE)
#
# custom_config = r'--oem 3 --psm 1'
# string = pytesseract.image_to_string(img, config=custom_config, lang="ara")
# dataDict = pytesseract.image_to_data(img, output_type=Output.DICT, config=custom_config, lang="ara")
# print(dataDict["text"])
# import cv2
# import pytesseract
# from pytesseract import Output
#
# img = cv2.imread('sampleImages/ArPageTest.jpg', cv2.IMREAD_GRAYSCALE)
#
# custom_config = r'--oem 3 --psm 1'
# string = pytesseract.image_to_string(img, config=custom_config, lang="ara")
#
# sectionsLst = string.split("\n\n")
# cleanSectionLst = []
#
# # create a new list of words separated by section
# for i in sectionsLst:
# cleanSectionLst.append(i.replace("\n", " ").split(" "))
#
# for i in range(len(cleanSectionLst)):
# if " " in cleanSectionLst[i]:
# cleanSectionLst[i] = list(filter(" ".__ne__, cleanSectionLst[i]))
# if "" in cleanSectionLst[i]:
# cleanSectionLst[i] = list(filter("".__ne__, cleanSectionLst[i]))
#
# if [] in cleanSectionLst:
# cleanSectionLst.remove([])
#
# # extra relevant data about the words on the image - coordinates
# dataDict = pytesseract.image_to_data(img, output_type=Output.DICT, config=custom_config, lang="ara")
#
# lstWords = list(dataDict["text"])
#
#
# def betterIdx(lst, word, num):
# # extracts the nth duplicate's index
# counter = 0
# lastIdx = 0
# for i in range(len(lst)):
# if lst[i] == word:
# counter += 1
# lastIdx = i
# if counter == num:
# return i
# return lastIdx
#
#
# # split each section with associated indices
# idxDict = {}
# dupWords = {}
#
# # links each section to the indices of the words in that section
# for i in cleanSectionLst:
# val = []
# for word in i:
# if word in lstWords:
# val.append(betterIdx(lstWords, word, dupWords.get(word, 1)))
# # to avoid issues with duplicates
# dupWords[word] = dupWords.get(word, 1) + 1
# idxDict[" ".join(i)] = val
#
# # extracts coordinates of each word in section
# coordDict = {}
# for key in idxDict:
# coordDict[key] = []
# for val in idxDict[key]:
# coordDict[key].append([dataDict["left"][val], dataDict["top"][val],
# dataDict["width"][val], dataDict["height"][val]])
#
# # find min and max coord of coordinates of words to get overall coordinates
# # for the whole section
#
# minMaxCoord = {}
#
# for elem in coordDict:
# minMaxCoord[elem] = [99999, 99999, 0, 0]
# for i in coordDict[elem]:
# # print(i[0], i[1], minMaxCoord[elem])
# if i[0] <= minMaxCoord[elem][0]:
# minMaxCoord[elem][0] = i[0]
#
# if i[0] + i[2] >= minMaxCoord[elem][2]:
# minMaxCoord[elem][2] = i[0] + i[2]
#
# if i[1] <= minMaxCoord[elem][1]:
# minMaxCoord[elem][1] = i[1]
#
# if i[1] + i[3] >= minMaxCoord[elem][3]:
# minMaxCoord[elem][3] = i[1] + i[3]
#
#
# for i in minMaxCoord:
# img = cv2.rectangle(img, (minMaxCoord[i][0], minMaxCoord[i][1]),
# (minMaxCoord[i][2], minMaxCoord[i][3]), (0, 0, 0), 2)
#
# cv2.imshow('img', img)
# cv2.waitKey(0)
import gtts
from playsound import playsound
import os
#
# tts = gtts.gTTS("My name is Iman and I love chocolate ", lang="en", slow=True)
# tts.save("instructionsArSpeech.mp3")
# playsound("instructionsArSpeech.mp3")
# os.remove("instructionsArSpeech.mp3")
from pynput import keyboard
#
#
# def on_press(key):
# if key.char == "a":
# print(key.char)
# try:
# if key.char == 'd':
# print("hi", key.char)
# except AttributeError:
# if key == 'd':
# print("bye", key.char)
#
# with keyboard.Listener(
# on_press=on_press) as listener:
# listener.join()
import tkinter as tk
import tkinter.font as tkFont
# gui = tk.Tk()
#
# gui.geometry("500x500")
# gui.title("Screen Reader for Visually Impaired")
#
# frame = tk.Frame(gui)
# frame.pack(side="top", expand=True, fill="both")
# instruction = """This application is a screen reader that helps you
# navigate websites. In order to use this screen reader
# go to the webpage that you want to be read and press alt.
# When you press alt your mouse will be moved to the bottom left
# of the page. In order to stop the program press Escape.
# You can now begin to use the screen reader. To change the dialect press D. In order to
# repeat the instructions in English press the spacebar. To repeat the instructions in arabic press A.
# To leave the instructions press L."""
# fontStyle = tkFont.Font(family="Lucida Grande", size=12)
# instructionLabel = tk.Label(frame,padx = 10, compound = tk.CENTER, text=instruction, font=fontStyle).pack(side="left")
# # instructionLabel.place()
# gui.mainloop()
# import tkinter as tk
#
# gui = tk.Tk()
# gui.geometry("500x500")
# gui.title("Screen Reader for Visually Impaired")
# instruction = """This application is a screen reader that helps you navigate websites.
# In order to use this screen reader go to the webpage that you want to be read
# and press alt. When you press alt your mouse will be moved to the
# bottom left of the page. In order to stop the program press
# Escape. You can now begin to use the screen reader.
# To change the dialect press D. In order to repeat the
# instructions in English press the spacebar. To repeat the
# instructions in arabic press A. To leave the instructions press L."""
#
# instrucLabel = tk.Label(gui, compound=tk.RIGHT, text=instruction, pady=50).pack(anchor="center")
#
# gui.mainloop()
# from pynput.mouse import Button, Controller
#
# mouse = Controller()
# mouseLoc = list(mouse.position)
# print(mouseLoc)
import gtts
from playsound import playsound
import pyautogui
from pynput import keyboard
from pynput import mouse
from pynput.mouse import Controller
import cv2
import pytesseract
from cmu_112_graphics import *
from pytesseract import Output
# urlImg = pyautogui.screenshot('fullPgScreenshot.png', region=(100, 50, 1000, 51))
#
# pngToJpg = cv2.imread('fullPgScreenshot.png')
#
# invert = cv2.bitwise_not(pngToJpg)
#
# cv2.imwrite('fullPgScreenshot.jpg', invert)
#
# # custom_config = r'--oem 3 --psm 1'
# string = pytesseract.image_to_string(invert)
# os.remove('fullPgScreenshot.png')
# # os.remove('fullPgScreenshot.jpg')
# print(string)
# for i in string.split(" "):
# if "/" in i or ".com" in i or ".org" in i:
# print(i)
# urlImg = pyautogui.screenshot('fullPgScreenshot.png', region=(0, 870, 720, 900))
# pngToJpg = cv2.imread('fullPgScreenshot.png')
# invert = cv2.bitwise_not(pngToJpg)
# cv2.imwrite('fullPgScreenshot.jpg', invert)
# os.remove('fullPgScreenshot.png')
# string = pytesseract.image_to_string(invert)
# print(string)
from imageai.Detection import ObjectDetection
# BUG FIX - for tensor flow error
# (GTS, StackOverFlow, 2021)
# https://stackoverflow.com/questions/66092421/how-to-rebuild-tensorflow-with-the-compiler-flags
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Using a pretrained model to detect the location of an image
# (Priyal, Medium, 2020)
# https://medium.datadriveninvestor.com/ai-object-detection-using-only-10-lines-of-code-imageai-89d3ba9886ea
#
# DOWNLOAD PREDEFINED MODEL FROM HERE - https://github.com/priyalwalpita/ai_object_detection
# execution_path = os.getcwd()
# detector = ObjectDetection()
# detector.setModelTypeAsRetinaNet()
# detector.setModelPath(os.path.join(execution_path, "resnet50_coco_best_v2.1.0.h5"))
# detector.loadModel()
# detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path, "sampleImages/imgTest.jpg"),
# output_image_path=os.path.join(execution_path, "imagenew.jpg"))
#
# # determine all image locations and what objects are in the image
# imgLocs = {}
# for (detect, idx) in zip(detections, range(len(detections))):
# # deals with the same object found in different locations
# if detect['name'] in imgLocs:
# imgLocs[detect['name'] + str(idx)] = detect['box_points']
# else:
# imgLocs[detect['name']] = detect['box_points']
#
#
# # to avoid repetition of already combined objects
#
# combinedImgsCheck = []
# combinedImgsNestLst = []
#
# # find images that contain more than one object, based on distance or overlap of bounding box
#
# for (obj1, idx) in zip(imgLocs, range(len(imgLocs))):
#
# similarCoord = [obj1]
#
# for (obj2, idx2) in zip(imgLocs, range(len(imgLocs))):
#
# if obj2 not in similarCoord and obj2 not in combinedImgsCheck:
#
# # extract coordinates from dictionary
#
# [img1XStart, img1YStart, img1XEnd, img1YEnd] = imgLocs[obj1]
# [img2XStart, img2YStart, img2XEnd, img2YEnd] = imgLocs[obj2]
#
# # 2 SCENARIOS - check if bounding boxes of objects overlap or are close to one another
#
# # 1 - overlap
#
# if ((img1XStart <= img2XStart <= img1XEnd) or (img1XStart <= img2XEnd <= img1XEnd)) and \
# (img1YStart <= img2YStart <= img1YEnd) or (img1YStart <= img2YEnd <= img1YEnd):
# similarCoord.append(obj2)
#
# # 2 - close to each other
#
# elif ((img1XEnd <= img2XStart <= img1XEnd + 10) or (img1XStart - 10 <= img2XEnd <= img1XStart)) and \
# ((img1YEnd <= img2YStart <= img1YEnd + 10) or (img1YStart - 10 <= img2YEnd <= img1YStart)):
# similarCoord.append(obj2)
#
# # combine into a list of combined images
#
# if len(similarCoord) > 1:
# combinedImgsNestLst.append(similarCoord)
# combinedImgsCheck += similarCoord
#
# # if multiple objects are found in an image; combine their locations and text into a user-friendly manner
#
# newImgLocs = {}
# for imgName in imgLocs:
# if imgName not in combinedImgsCheck:
# newImgLocs[imgName] = imgLocs[imgName]
# else:
# for combinedImgLst in combinedImgsNestLst:
# combinedName = " ".join(combinedImgLst)
# allCoord = [[], [], [], []]
#
# # combine the coordinates of all objects that overlap or close to each other to make one bigger object
# for combinedImgName in combinedImgLst:
# allCoord[0].append(imgLocs[combinedImgName][0])
# allCoord[1].append(imgLocs[combinedImgName][1])
# allCoord[2].append(imgLocs[combinedImgName][2])
# allCoord[3].append(imgLocs[combinedImgName][3])
#
# # combine the coordinates
# combinedCoord = [min(allCoord[0]), min(allCoord[1]), max(allCoord[2]), max(allCoord[3])]
#
# newImgLocs[combinedName] = combinedCoord
#
# # change text to be user friendly with use in text to speech
# finalImgLocs = {}
#
# for name in newImgLocs:
# nameLst = name.split(" ")
# if len(nameLst) > 1:
# firstPtLst = " ".join(nameLst[:-1])
# secondPtLst = nameLst[-1]
# finalImgLocs[f"You are hovering over an image that contains {firstPtLst} and {secondPtLst}"] = newImgLocs[name]
# else:
# finalImgLocs[f"You are hovering over an image that contains {nameLst[0]}"] = newImgLocs[name]
#
# print(finalImgLocs)
# img = cv2.imread('sampleImages/imgTest.jpg')
#
# for i in newImgLocs:
# img = cv2.rectangle(img, (newImgLocs[i][0], newImgLocs[i][1]),
# (newImgLocs[i][2], newImgLocs[i][3]), (0, 0, 0), 2)
#
# cv2.imshow('img', img)
# cv2.waitKey(0)
from googletrans import Translator, constants
translator = Translator()
translation = translator.translate("Hello", dest="ar")
print(translation.text)