/
Final.py
673 lines (518 loc) · 16.5 KB
/
Final.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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
import time
import imutils
import Image
from espeak import espeak
from PyDictionary import PyDictionary
from collections import deque
import numpy as np
import argparse
import glob
import cv2
from matplotlib import pyplot as plt
import pytesseract
import os
rot = 0
z = 0
low1 = (25, 60, 195)
high1 = (50, 125, 230)
def page (t,stop):
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=32,
help="max buffer size")
args = vars(ap.parse_args())
# define the lower and upper boundaries of the "green"
# ball in the HSV color space
greenLower = low1
greenUpper = high1
# initialize the list of tracked points, the frame counter,
# and the coordinate deltas
pts = deque(maxlen=args["buffer"])
counter = 0
(dX, dY) = (0 , 0)
direction = ""
k=1
l=0
p=''
# if a video path was not supplied, grab the reference
# to the webcam
if not args.get("video", False):
camera = cv2.VideoCapture(0)
# otherwise, grab a reference to the video file
else:
camera = cv2.VideoCapture(args["video"])
# keep looping
while True:
#will start tracking when q is pressed, until then just display the video
"""if l ==1:
while True:
ret,frame2 = camera.read()
frame8=cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
rows,cols = frame8.shape
#frame2 = cv2.warpAffine(frame2,Rotate.rotate(),(cols,rows))
cv2.imshow('frame', frame2)
p=cv2.waitKey(30) & 0xFF
if p==ord('q'):
l=0
break """
if l==0:
ret,frame0 = camera.read()
#frame8=cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY)
#rows,cols = frame8.shape
#frame0 = cv2.warpAffine(frame0,Rotate.rotate(),(cols,rows))
l=-1
# grab the current frame
(grabbed, frame) = camera.read()
#frame8=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#rows,cols = frame8.shape
#frame = cv2.warpAffine(frame,Rotate.rotate(),(cols,rows))
# if we are viewing a video and we did not grab a frame,
# then we have reached the end of the video
if args.get("video") and not grabbed:
break
# resize the frame, blur it, and convert it to the HSV
# color space
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
# construct a mask for the color "green", then perform
# a series of dilations and erosions to remove any small
# blobs left in the mask
mask = cv2.inRange(hsv, greenLower, greenUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cv2.imshow('mask', mask)
# find contours in the mask and initialize the current
# (x, y) center of the ball
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[-2]
center = None
# only proceed if at least one contour was found
if len(cnts) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
if k==1 :
a = int(M["m10"] / M["m00"])
b = int(M["m01"] / M["m00"])
k=0
# only proceed if the radius meets a minimum size
if radius > 10:
#update the list of tracked points
pts.appendleft(center)
if len(pts)>10 :
# loop over the set of tracked points
for i in np.arange(1, len(pts)):
# if either of the tracked points are None, ignore
# them
if pts[i - 1] is None or pts[i] is None:
continue
# check to see if enough points have been accumulated in
# the buffer
if i == 1 and pts[-10] is not None:
# compute the difference between the x and y
# coordinates
dX = pts[-10][0] - pts[i][0]
dY = pts[-10][1] - pts[i][1]
# show the frame to our screen and increment the frame counter
cv2.imshow("frame", frame)
key = cv2.waitKey(1) & 0xFF
counter += 1
t=t-1
# if the 'q' key is pressed, or if the object slows down, loop is exited and the cropped part is displayed
if (key == ord('q')) or (len(pts)>24 and np.abs(dX) < 10 and np.abs(dY) < 10):
#c = int(M["m10"] / M["m00"])
#d = int(M["m01"] / M["m00"])
#check if it is an image
if stop == 0:
espeak.synth("now show me the diagonally opposite corner")
time.sleep(4)
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()
page(400 ,1);
break
if stop == 1:
espeak.synth("the camera is ready to be used")
time.sleep(5)
camera.release()
cv2.destroyAllWindows()
break
#cv2.imshow('pic2',crop_img)
#cv2.imshow('base',frame0)
#cv2.waitKey(0)
if t == 0:
espeak.synth("that endpoint is not visible to me,please try again")
time.sleep(4)
camera.release()
cv2.destroyAllWindows()
page(400,stop);
break
return
def rotate():
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=32,
help="max buffer size")
args = vars(ap.parse_args())
# define the lower and upper boundaries of the "green"
# ball in the HSV color space
lower = low1
upper = high1
# initialize the list of tracked points,
# and the coordinate deltas
pts = deque(maxlen=args["buffer"])
(dX, dY) = (0, 0)
# if a video path was not supplied, grab the reference
# to the webcam
if not args.get("video", False):
camera = cv2.VideoCapture(0)
# otherwise, grab a reference to the video file
else:
camera = cv2.VideoCapture(args["video"])
espeak.synth("Hi, I am Jarvis. Your life, or the bottom left corner of your page.")
time.sleep(0.5)
# keep looping
while True:
# grab the current frame
(grabbed, frame) = camera.read()
# if we are viewing a video and we did not grab a frame,
# then we have reached the end of the video
if args.get("video") and not grabbed:
break
# resize the frame, blur it, and convert it to the HSV
# color space
frame = imutils.resize(frame, width=600)
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
# construct a mask for required colour, then perform
# a series of dilations and erosions to remove any small
# blobs left in the mask
mask = cv2.inRange(hsv, lower, upper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# find contours in the mask and initialize the current
# (x, y) center of the ball
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[-2]
center = None
# only proceed if at least one contour was found
if len(cnts) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
# only proceed if the radius meets a minimum size
if radius > 10:
# update the list of tracked points
pts.appendleft(center)
if len(pts)>10 :
if pts[1] is not None:
if pts[-10] is not None:
# compute the difference between the x and y
# coordinates
dX = pts[-10][0] - pts[1][0]
dY = pts[-10][1] - pts[1][1]
key = cv2.waitKey(1) & 0xFF
cv2.imshow("frame", frame)
#cv2.imshow("frame2", frame2)
# if the 'q' key is pressed, or if the object slows down, the variable is updated and the function ends
if ((key == ord('q')) or (len(pts)>30 and np.abs(dX) < 25 and np.abs(dY) < 25)):
c = int(M["m10"] / M["m00"])
d = int(M["m01"] / M["m00"])
if c < 300.0 and d < 225.0: #the values are dependant upon the pixels of the frame
rot= 1;
print 2
return cv2.getRotationMatrix2D((600/2,450/2),90,1);
elif c < 300.0 and d >= 225.0:
rot= 2;
print 2
return cv2.getRotationMatrix2D((600/2,450/2),0,1);
elif c >= 300.0 and d < 225.0:
rot=3;
print 2
return cv2.getRotationMatrix2D((600/2,450/2),180,1);
elif c >= 300.0 and d >= 225.0:
rot=4;
print 2
return cv2.getRotationMatrix2D((600/2,450/2),270,1);
# cleanup the camera and close any open windows
camera.release()
return;
def crop (M2):
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=32,
help="max buffer size")
args = vars(ap.parse_args())
# define the lower and upper boundaries of the "green"
# ball in the HSV color space
greenLower = low1
greenUpper = high1
# initialize the list of tracked points, the frame counter,
# and the coordinate deltas
pts = deque(maxlen=args["buffer"])
counter = 0
(dX, dY) = (0 , 0)
direction = ""
k=1
l=1
p=''
# if a video path was not supplied, grab the reference
# to the webcam
if not args.get("video", False):
camera = cv2.VideoCapture(0)
# otherwise, grab a reference to the video file
else:
camera = cv2.VideoCapture(args["video"])
# keep looping
espeak.synth("to start, press q")
time.sleep(2)
while True:
#will start tracking when q is pressed, until then just display the video
if l ==1:
while True:
ret,frame2 = camera.read()
cv2.imshow('frame', frame2)
p=cv2.waitKey(30) & 0xFF
if p==ord('q'):
l=0
break
if l==0:
espeak.synth("start")
time.sleep(0.2)
ret,frame0 = camera.read()
frame0 = imutils.resize(frame0, width=600)
frame0 = cv2.warpAffine(frame0,M2,(600,450))
l=-1
# grab the current frame
(grabbed, frame) = camera.read()
# if we are viewing a video and we did not grab a frame,
# then we have reached the end of the video
if args.get("video") and not grabbed:
break
# resize the frame, blur it, and convert it to the HSV
# color space
frame = imutils.resize(frame, width=600)
frame = cv2.warpAffine(frame,M2,(600,450))
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
# construct a mask for the color "green", then perform
# a series of dilations and erosions to remove any small
# blobs left in the mask
mask = cv2.inRange(hsv, greenLower, greenUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cv2.imshow('mask', mask)
# find contours in the mask and initialize the current
# (x, y) center of the ball
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[-2]
center = None
# only proceed if at least one contour was found
if len(cnts) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
if k==1 :
a = int(M["m10"] / M["m00"])
b = int(M["m01"] / M["m00"])
k=0
# only proceed if the radius meets a minimum size
if radius > 10:
#update the list of tracked points
cv2.circle(frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
pts.appendleft(center)
if len(pts)>10 :
# loop over the set of tracked points
for i in np.arange(1, len(pts)):
# if either of the tracked points are None, ignore
# them
if pts[i - 1] is None or pts[i] is None:
continue
# check to see if enough points have been accumulated in
# the buffer
if i == 1 and pts[-10] is not None:
# compute the difference between the x and y
# coordinates
dX = pts[-10][0] - pts[i][0]
dY = pts[-10][1] - pts[i][1]
# show the frame to our screen and increment the frame counter
cv2.imshow("frame", frame)
key = cv2.waitKey(1) & 0xFF
counter += 1
if (l==-1 and (np.abs(dX) > 15 or np.abs(dY) > 15)):
l=-2
# if the 'q' key is pressed, or if the object slows down, loop is exited and the cropped part is displayed
if (l==-2 and key == ord('q')) or (l==-2 and len(pts)>24 and np.abs(dX) < 10 and np.abs(dY) < 10):
c = int(M["m10"] / M["m00"])
d = int(M["m01"] / M["m00"])
print a
print b
print c
print d
#check if it is an image
#print d-b
#print len(pts)
if (np.abs(d-b)>35):
espeak.synth("image")
time.sleep(1)
crop_img = frame0[d:b,a:c]
camera.release()
cv2.imshow('cropped', crop_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
return 0;
#else treat it as an underline of a word
else:
espeak.synth("word")
time.sleep(1)
crop_img = frame0[b-60:b-5,a:c]
crop_img = cntour(crop_img)
z=2
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()
print 3
return crop_img;
def cntour (crop_img):
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=32,
help="max buffer size")
args = vars(ap.parse_args())
# define the lower and upper boundaries of the "green"
# ball in the HSV color space, then initialize the
# list of tracked points
greenLower = (0, 0, 100)
greenUpper = (255, 100, 255)
pts = deque(maxlen=args["buffer"])
# resize the frame, blur it, and convert it to the HSV
# color space
blurred = cv2.GaussianBlur(crop_img, (11, 11), 0)
hsv = cv2.cvtColor(crop_img, cv2.COLOR_BGR2HSV)
# construct a mask for the color "green", then perform
# a series of dilations and erosions to remove any small
# blobs left in the mask
cv2.imshow("window", hsv)
cv2.waitKey(3000)
mask = cv2.inRange(hsv, greenLower, greenUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(cnts) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
e = int(M["m10"] / M["m00"])
f = int(M["m01"] / M["m00"])
cv2.imwrite('pic3.jpg',crop_img)
crop_img2 = crop_img[f+5:1000000,0:10000000]
#cv2.imwrite('pic3.jpg',crop_img2)
return crop_img2;
def ocr(img):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
a = np.array([0,0,140])
b = np.array([180,100,255])
mask = cv2.inRange(hsv, a, b)
cv2.imshow('before', mask)
#mask1 = img
#mask = cv2.dilate(mask, None, iterations=2)
#mask = cv2.erode(mask, None, iterations=2)
#mask1 = cv2.fastNlMeansDenoising(mask, mask1, 200)
cv2.imshow('after', mask)
cv2.imshow('image', gray)
cv2.waitKey(0)
cv2.imwrite('test.jpg', img)
s= pytesseract.image_to_string(Image.open('test.jpg'))
#os.remove('test.jpg')
print 4
return s
def dictionary(word):
i=0
#while (1):
print word
dictionary=PyDictionary()
dict=dictionary.meaning(word)
if dict is not None:
espeak.synth("your word is " + word)
time.sleep(2.5)
if ( dict.has_key('Adjective')) :
s= dict['Adjective']
if len(s)>=i :
print s[i]
l= len(s[i])
t = l /12.0
espeak.synth("(adjective)" + s[i])
time.sleep(t)
if dict.has_key('Noun') :
s= dict['Noun']
if len(s)>=i :
print s[i]
l= len(s[0])
t = l /12.0
espeak.synth("(NOUN)" + s[i])
time.sleep(t)
if dict.has_key('Verb') :
s= dict['Verb']
if len(s)>=i :
print s[i]
l= len(s[i])
t = l /12.0
espeak.synth("VERB" + s[i])
time.sleep(t)
if dict.has_key('Adverb') :
s= dict['Adverb']
if len(s)>=i :
print s[i]
l= len(s[i])
t = l /12.0
espeak.synth("(ADVERB)" + s[i])
time.sleep(t)
if dict.has_key('Preposition') :
s= dict['Preposition']
if len(s)>=i :
print s[i]
l= len(s[i])
t = l /12.0
espeak.synth("(PREPO)" + s[i])
time.sleep(t)
print 5
#espeak.synth("If alternate meaning required, give a double tap within the next 3 seconds")
#audio trigger will be awaited here, after message for one, in case user didnt get meaning that was wanted
#if received, then i++
def main():
#espeak.synth("show me an end point of book")
#time.sleep(5)
#page(400,0)
print 1
M = rotate()
crop_img = crop(M)
if crop_img is not 0:
s=ocr(crop_img)
dictionary(s)
main()