-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSurveillance.NWB.py
973 lines (836 loc) · 39.8 KB
/
Surveillance.NWB.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# _____________________HEADER FILES______________________
import tkinter
from tkinter import*
from tkinter import ttk
from tkinter import filedialog
from imutils.video import FPS
from _cffi_backend import callback
from PIL import ImageTk, Image
import cv2
from cv2 import *
import numpy as np
import sys
import time
import argparse
import imutils
import keyboard
from pathlib import Path
from utils import *
import time
from skimage.restoration import wiener
from scipy.special import j1
# _____________________USER-DEFINED FUNCTIONS______________________
kernel_d = np.ones((3, 3), np.uint8)
kernel_e = np.ones((3, 3), np.uint8)
kernel_gauss = (3, 3)
is_blur = False # initializing_boolean_variables
is_close = True # initializing_boolean_variables
is_draw_ct = False # initializing_boolean_variables
fac = 2 # initializing_integer_variables
isVideoCaptureOpen = False # boolean flag to keep a check of the video capture
# ______________________OUTPUT_________________________________________
parent_dir = os.getcwd()
directory = "Results"
out_path = os.path.join(parent_dir, directory)
try:
os.mkdir(out_path)
except OSError as error:
pass
# ___________________INITALIZING THE GUI WINDOW______________________
window = Tk()
window.configure(background="grey64")
window.title("Surveillance System")
window.resizable(height=None, width=None)
window.geometry('1300x480')
# _______________SETTING VARIBALES TO CHECK STATE OF BUTTON (CHECKED OR UNCHECKED)______________________
current_value1 = IntVar()
current_value2 = IntVar()
# _______________________Global Variables__________________________
path = ""
flag_for_browse = False
source_file = ""
capture = VideoCapture(0)
frame_width = 0
frame_height = 0
ROI_enhanced_arr = []
Combined_frames = []
object_frames = []
iterFPS = 0
top = None
fps = 0
fat = 0
def get_current_value1():
return int('{}'.format(current_value1.get()))
def slider_changed1(event):
value_label1.configure(text=get_current_value1())
slider_label1 = Label(window, text='Dilation', font=(
"Times New Roman", 12), fg="black", bg="grey64").place(x=832, y=52)
value_label1 = ttk.Label(window, text=get_current_value1())
slider1 = ttk.Scale(window, from_=0, to=20, orient='horizontal',
command=slider_changed1, variable=current_value1)
slider1.set(13)
slider1.place(x=890, y=50)
value_label1.place(x=995, y=52)
def get_current_value2():
return int('{}'.format(current_value2.get()))
def slider_changed2(event2):
value_label2.configure(text=get_current_value2())
slider_label2 = Label(window, text='Erosion', font=(
"Times New Roman", 12), fg="black", bg="grey64").place(x=832, y=82)
value_label2 = ttk.Label(window, text=get_current_value2())
slider2 = ttk.Scale(window, from_=0, to=20, orient='horizontal',
command=slider_changed2, variable=current_value2)
slider2.set(5)
slider2.place(x=890, y=82)
value_label2.place(x=995, y=82)
# _____________________CREATING BUTTONS______________________
title = Label(window, text="Surveillance System", font=(
"Times New Roman", 18, 'bold'), fg="black", bg="grey64").place(x=495, y=10)
label_file_explorer = Label(window, text="", fg="blue")
label_file_explorer.place(x=20, y=45)
# _____________________HEADER______________________
# title = Label(window, text="Surveillance System", font=(
# "Times New Roman", 18, 'bold'), fg="black", bg="grey64").place(x=495, y=10)
#label_file_explorer = Label(window, text = "", fg = "blue")
# label_file_explorer.place(x=20,y=60)
input_frame = LabelFrame(window.geometry('500x700'), text="Input", font=(
"Times New Roman", 18, 'bold'), bg="grey64")
input_frame.pack(side='left', expand='yes')
L1 = Label(input_frame, bg="grey64")
L1.pack()
output_frame = LabelFrame(window.geometry('500x700'), text="Output", font=(
"Times New Roman", 18, 'bold'), bg="grey64")
output_frame.pack(side='right', expand='yes')
L2 = Label(output_frame, bg="grey64")
L2.pack()
displayVar = StringVar()
sample_text_fps = Label(window, bg='grey64', text="FPS: ",
font=("Times New Roman", 12, 'bold'))
sample_text_fps.place(x=40, y=85)
text_fps = Label(window, bg='grey64', textvariable=displayVar,
font=("Times New Roman", 12))
text_fps.place(x=75, y=85)
displayVarFAT = StringVar()
sample_text_fat = Label(window, bg='grey64', text="FAT: ",
font=("Times New Roman", 12, 'bold'))
sample_text_fat.place(x=40, y=115)
text_fat = Label(window, bg='grey64', textvariable=displayVarFAT,
font=("Times New Roman", 12))
text_fat.place(x=75, y=115)
displayVarPath = StringVar()
sample_text_path = Label(window, bg='grey64', text="Path To Output: ",
font=("Times New Roman", 12, 'bold'))
sample_text_path.place(x=40, y=145)
text_path = Label(window, bg='grey64', textvariable=displayVarPath,
font=("Times New Roman", 12))
text_path.place(x=160, y=145)
displayVar.set(str(0))
displayVarFAT.set(str(0))
un_entry = StringVar()
passwd_entry = StringVar()
ip_add_entry = StringVar()
# ___________________Object detection code___________________
def open_popup():
global top
top = Toplevel(window)
top.geometry("350x250")
top.resizable(0, 0)
top.title("Choose Video Source")
var = IntVar()
or_label = Label(top, text="OR", font=("Times New Roman", 12, 'bold')).place(x=20, y=125)
R1 = Radiobutton(top, text="Browse Files", font=("Times New Roman", 12, 'bold'),variable=var,
value=1, command=switch_flag_for_browse).place(x=10, y=160)
username = Label(top, text="Username",font=("Times New Roman", 12)).place(x=10, y=10)
password = Label(top, text="Password",font=("Times New Roman", 12)).place(x=10, y=50)
ip_address = Label(top, text="IP Address",font=("Times New Roman", 12)).place(x=10, y=90)
E1 = Entry(top, bd=2, textvariable=un_entry).place(x=100, y=10)
E2 = Entry(top, bd=2, textvariable=passwd_entry).place(x=100, y=50)
E3 = Entry(top, bd=2, textvariable=ip_add_entry).place(x=100, y=90)
B1=Button(top, text="Submit", font=("Times New Roman", 12,'bold'),command=submit)
B1.place(x=250, y=190)
def switch_flag_for_browse():
global flag_for_browse
flag_for_browse = True
print("FLAG FOR BROWSE SWITCHED")
def browseFiles():
source_file = filedialog.askopenfilename(
initialdir="/", title="Select a File", filetypes=[('All Files', '.*')], parent=window)
label_file_explorer.configure(text="File: "+source_file)
path = source_file
print(path)
return source_file
def submit():
global path
un = un_entry.get()
pw = passwd_entry.get()
ip = ip_add_entry.get()
url = f'rtsp://{un}:{pw}@{ip}:554/Streaming/Channels/102'
# print(url)
path = url
print(path)
un_entry.set("")
passwd_entry.set("")
ip_add_entry.set("")
top.destroy()
def loadVideo(videopath):
global frame_width, frame_height
ImagesSequence = []
i = 0
start = time.time()
capture = cv2.VideoCapture(path)
frame_width = int(capture.get(3))
frame_height = int(capture.get(4))
while(True):
ret, frame = capture.read()
i += 1
end = time.time()
if ret == True:
#frame = cv2.flip(frame, 1)
ImagesSequence.append(cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY))
cv2.imshow('gray', cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY))
fps = i/(end-start)
displayVar.set(str(int(fps)))
if cv2.waitKey(25) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
return ImagesSequence
def write_video(frames_list, fps, type, detur=False):
if detur:
Frames_BGR = [cv2.cvtColor(Frame, cv2.COLOR_GRAY2BGR)
for Frame in frames_list]
displayVarPath.set(str(f'{out_path}/{type}.mp4'))
out = cv2.VideoWriter(f'{out_path}/{type}.mp4', cv2.VideoWriter_fourcc(*'MP4V'), fps, (frames_list[0].shape[1],
frames_list[0].shape[0]))
for i in range(len(frames_list)):
if detur:
out.write(Frames_BGR[i].astype(np.uint8))
else:
out.write(frames_list[i])
out.release()
def toggleCapture():
try:
global capture
global path
if flag_for_browse:
path = browseFiles()
print("OBJ DETECT PATH: ", path)
capture = VideoCapture(path)
except:
print("Exception Found")
def objdetect():
global iterFPS
global object_frames
isVideoCaptureOpen = True
i = 0
while True:
start = time.time()
(ret_old, old_frame) = capture.read()
iterFPS += 1
gray_oldframe = cvtColor(old_frame, COLOR_BGR2GRAY)
if(is_blur):
gray_oldframe = GaussianBlur(gray_oldframe, kernel_gauss, 0)
oldBlurMatrix = np.float32(gray_oldframe)
accumulateWeighted(gray_oldframe, oldBlurMatrix, 0.003)
while True:
ret, frame = capture.read()
gray_frame = cvtColor(frame, COLOR_BGR2GRAY)
if(is_blur):
newBlur_frame = GaussianBlur(gray_frame, kernel_gauss, 0)
else:
newBlur_frame = gray_frame
newBlurMatrix = np.float32(newBlur_frame)
minusMatrix = absdiff(newBlurMatrix, oldBlurMatrix)
ret, minus_frame = threshold(minusMatrix, 60, 255.0, THRESH_BINARY)
accumulateWeighted(newBlurMatrix, oldBlurMatrix, 0.02)
inp = ImageTk.PhotoImage(Image.fromarray(frame))
L1['image'] = inp
# imshow('Input', frame)
# drawRectangle(frame, minus_frame)
if(is_blur):
minus_frame = GaussianBlur(minus_frame, kernel_gauss, 0)
minus_Matrix = np.float32(minus_frame)
if(is_close):
for i in range(get_current_value1()):
minus_Matrix = dilate(minus_Matrix, kernel_d)
for i in range(get_current_value2()):
minus_Matrix = erode(minus_Matrix, kernel_e)
minus_Matrix = np.clip(minus_Matrix, 0, 255)
minus_Matrix = np.array(minus_Matrix, np.uint8)
contours, hierarchy = findContours(
minus_Matrix.copy(), RETR_TREE, CHAIN_APPROX_SIMPLE)
for c in contours:
(x, y, w, h) = boundingRect(c)
rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if(is_draw_ct):
drawContours(frame, contours, -1, (0, 255, 255), 1)
# imshow('Object_Detection', frame)
end = time.time()
object_frames.append(frame)
frame = ImageTk.PhotoImage(Image.fromarray(frame))
fps = iterFPS/(end-start)
# displayVar.set(str(int(np.random.randint(18, 20))))
displayVar.set(str(int(fps)))
L2['image'] = frame
window.update()
if keyboard.is_pressed('q') and isVideoCaptureOpen == True:
try:
isVideoCaptureOpen = False
capture.release()
L1.config(image='')
L2.config(image='')
displayVar.set(str(0))
write_video(object_frames, 10, 'object_detect')
print("Capture released")
return
except:
print("Some error has occured")
def deturbulence():
try:
global path
if flag_for_browse:
path = browseFiles()
print("deturbulenceT PATH: ", path)
except:
print("Exception Found")
global ROI_enhanced_arr
dataType = np.float32
N_FirstReference = 10
L = 11
patch_size = (L, L) # (y,x) [pixels]. isoplanatic region
patch_half_size = (
int((patch_size[0] - 1) / 2), int((patch_size[1] - 1) / 2))
patches_shift = 1 # when equals to one we get full overlap.
# (y,x). for each side: up/down/left/right
registration_interval = (15, 15)
R = 0.08 # iterativeAverageConstant
m_lambda0 = 0.55 * 10 ** -6
m_aperture = 0.06
m_focal_length = 250 * 10 ** -3
fno = m_focal_length / m_aperture
# 3 options: 1. via Lucky region for N_firstRef frames, 2. mean of N_firstRef frames 3. first frame.
cap = cv2.VideoCapture(path)
ImagesSequenceList = []
ROI_arr_to_save = []
ROI_enhanced_arr_to_save = []
itr_fps = 0
start = time.time()
# ImagesSequence = loadVideo(0)
while True:
ret, frame = cap.read()
itr_fps += 1
end = time.time()
if keyboard.is_pressed('q'):
L1.config(image='')
L2.config(image='')
displayVar.set(str(0))
displayVarFAT.set(str(0))
cap.release()
concatenatedVid = [np.hstack((ROI_arr_to_save[i], np.zeros(
(ROI_arr_to_save[0].shape[0], 10)), ROI_enhanced_arr_to_save[i])).astype(np.float32) for i in range(len(ROI_arr_to_save))]
write_video(concatenatedVid, 2, 'deturbulence', True)
return
if ret:
fps = itr_fps/(end-start)
displayVar.set(str(int(fps)))
#frame = cv2.flip(frame, 1)
# np.append(ImagesSequence, cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY))
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
if(len(ImagesSequenceList) > 10):
ImagesSequenceList.clear()
ImagesSequenceList.append(frame)
ImagesSequence = np.array(ImagesSequenceList).astype(dataType)
# roi = selectROI(ImagesSequence[0], resize_factor=2)
# print("LEN OF IMAGE SEQ:", len(ImagesSequence))
roi = (0, 0, ImagesSequence[0].shape[0],
ImagesSequence[0].shape[1])
ROI_coord = roi
ROI_coord = (ROI_coord[1], ROI_coord[0], patch_size[1] * int(ROI_coord[3] / patch_size[1]),
patch_size[0] * int(ROI_coord[2] / patch_size[0])) # now roi[0] - rows!
ROI_arr = []
enhancedFrames = []
# option 2: Mean of N_FirstReference frames.
ReferenceFrame = np.mean(ImagesSequence[:N_FirstReference], axis=0)
startRegistrationFrame = N_FirstReference
enhancedFrames.append(ReferenceFrame)
i = 0
if len(ImagesSequenceList) > 10:
for frame in ImagesSequence[startRegistrationFrame:]:
t = time.time()
enhancedFrame = np.copy(frame)
ROI = frame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
ROI_arr.append(ROI*255.0/ROI.max())
ROI_arr_to_save.append(ROI*255.0/ROI.max())
no_rows_Cropped_Frame, no_cols_Cropped_Frame = \
(ROI_coord[2] + 2 * registration_interval[0],
ROI_coord[3] + 2 * registration_interval[1])
ReferenceFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] = \
(1 - R) * ReferenceFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] + \
R * frame[ROI_coord[0]: ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
ROI_registered = ReferenceFrame[ROI_coord[0]:ROI_coord[0] +
ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
m_lambda0 = 0.55 * 10 ** -6
m_aperture_diameter = 0.055
m_focal_length = 250 * 10 ** -3
fno = m_focal_length / m_aperture_diameter
ROI_reg_norm = ROI_registered / 255
k = (2 * np.pi) / m_lambda0
Io = 1.0
L = 250
X = np.arange(-m_aperture_diameter/2,
m_aperture_diameter/2, m_aperture_diameter/70)
Y = X
XX, YY = np.meshgrid(X, Y)
AiryDisk = np.zeros(XX.shape)
# print("SHAPE: ", AiryDisk.shape)
q = np.sqrt((XX-np.mean(Y)) ** 2 + (YY-np.mean(Y)) ** 2)
beta = k * m_aperture_diameter * q / 2 / L
AiryDisk = Io * (2 * j1(beta) / beta) ** 2
AiryDisk_normalized = AiryDisk/AiryDisk.max()
deblurredROI_wiener = wiener(
ROI_reg_norm, psf=AiryDisk, balance=7)
deblurredROI = deblurredROI_wiener
deblurredROI = deblurredROI / deblurredROI.max() * 255.0
enhancedFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] = np.abs(deblurredROI)
ROI_enhanced_arr.clear()
ROI_enhanced_arr.append(deblurredROI)
ROI_enhanced_arr_to_save.append(deblurredROI)
enhancedFrames.append(enhancedFrame)
displayVarFAT.set(str("{:.3f}".format(time.time() - t)))
# print("LEN OF ROI_arr: ", len(ROI_arr))
# print("LEN OF ROI_enhanced_arr: ", len(ROI_enhanced_arr))
try:
inp_roi = ImageTk.PhotoImage(
Image.fromarray(ROI_arr[i].astype(np.uint8)))
out_roi = ImageTk.PhotoImage(Image.fromarray(
ROI_enhanced_arr[i].astype(np.uint8)))
except:
L1.config(image='')
L2.config(image='')
return
L1['image'] = inp_roi
L2['image'] = out_roi
window.update()
i += 1
def endeturbulence():
global path
if flag_for_browse:
path = browseFiles()
print("deturbulenceT PATH: ", path)
dataType = np.float32
N_FirstReference = 10
L = 11
patch_size = (L, L)
patch_half_size = (
int((patch_size[0] - 1) / 2), int((patch_size[1] - 1) / 2))
patches_shift = 1
registration_interval = (15, 15)
R = 0.08
m_lambda0 = 0.55 * 10 ** -6
m_aperture = 0.06
m_focal_length = 250 * 10 ** -3
fno = m_focal_length / m_aperture
readVideo = 1
ReferenceInitializationOpt = 2
ImagesSequence = loadVideo(
'rtsp://Guest:[email protected]:554/Streaming/Channels/102')
ImagesSequence = np.array(ImagesSequence).astype(dataType)
roi = selectROI(ImagesSequence[0], resize_factor=2)
roi_plate_250 = (1092, 830, 564, 228)
roi_test = (310, 279, 200, 128)
if readVideo:
ROI_coord = roi
else:
ROI_coord = roi_plate_250
ROI_coord = (ROI_coord[1], ROI_coord[0], patch_size[1] * int(ROI_coord[3] / patch_size[1]),
patch_size[0] * int(ROI_coord[2] / patch_size[0])) # now roi[0] - rows!
ROI_arr = []
ROI_enhanced_arr = []
enhancedFrames = []
if ReferenceInitializationOpt == 1: # option 1: "Lucky" reference frame.
FusedPatch = MaxSharpnessFusedPatch([frame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
for frame in ImagesSequence[:N_FirstReference]], patch_half_size)
ReferenceFrame = ImagesSequence[N_FirstReference]
ReferenceFrame[ROI_coord[0] + patch_half_size[0]:ROI_coord[0] + ROI_coord[2] - patch_half_size[0],
ROI_coord[1] + patch_half_size[1]:ROI_coord[1] + ROI_coord[3] - patch_half_size[1]] = FusedPatch
startRegistrationFrame = N_FirstReference
# option 2: Mean of N_FirstReference frames.
elif ReferenceInitializationOpt == 2:
ReferenceFrame = np.mean(ImagesSequence[:N_FirstReference], axis=0)
startRegistrationFrame = N_FirstReference
elif ReferenceInitializationOpt == 3: # option 3: first frame
ReferenceFrame = ImagesSequence[0]
startRegistrationFrame = 1
else:
assert Exception("only values 1, 2 or 3 are acceptable")
# showImage(ReferenceFrame.astype(np.uint8))
enhancedFrames.append(ReferenceFrame)
i = 0
for frame in ImagesSequence[startRegistrationFrame:]:
t = time.time()
enhancedFrame = np.copy(frame)
ROI = frame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
ROI_arr.append(ROI*255.0/ROI.max())
# Image Registration via optical flow
no_rows_Cropped_Frame, no_cols_Cropped_Frame = \
(ROI_coord[2] + 2 * registration_interval[0],
ROI_coord[3] + 2 * registration_interval[1])
u, v = optical_flow_tvl1(
ReferenceFrame[ROI_coord[0] - registration_interval[0]:ROI_coord[0] + ROI_coord[2] + registration_interval[0],
ROI_coord[1] - registration_interval[1]:ROI_coord[1] + ROI_coord[3] + registration_interval[1]],
enhancedFrame[ROI_coord[0] - registration_interval[0]:ROI_coord[0] + ROI_coord[2] + registration_interval[0],
ROI_coord[1] - registration_interval[1]:ROI_coord[1] + ROI_coord[3] + registration_interval[1]],
attachment=10, tightness=0.3, num_warp=3, num_iter=5, tol=4e-4, prefilter=False)
row_coords, col_coords = np.meshgrid(np.arange(no_rows_Cropped_Frame), np.arange(no_cols_Cropped_Frame),
indexing='ij')
warp(enhancedFrame[ROI_coord[0] - registration_interval[0]:ROI_coord[0] + ROI_coord[2] + registration_interval[0],
ROI_coord[1] - registration_interval[1]:ROI_coord[1] + ROI_coord[3] + registration_interval[1]],
np.array([row_coords + v, col_coords + u]), mode='nearest', preserve_range=True).astype(dataType)
# Iterative averaging ROI
ReferenceFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] = \
(1 - R) * ReferenceFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] + \
R * frame[ROI_coord[0]: ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
ROI_registered = ReferenceFrame[ROI_coord[0]:ROI_coord[0] +
ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
m_lambda0 = 0.55 * 10 ** -6
m_aperture_diameter = 0.055
m_focal_length = 250 * 10 ** -3
fno = m_focal_length / m_aperture_diameter
ROI_reg_norm = ROI_registered / 255
k = (2 * np.pi) / m_lambda0 # wavenumber of light in vacuum
Io = 1.0 # relative intensity
L = 250 # distance of screen from aperture
X = np.arange(-m_aperture_diameter/2, m_aperture_diameter /
2, m_aperture_diameter/70) # pupil coordinates
Y = X
XX, YY = np.meshgrid(X, Y)
AiryDisk = np.zeros(XX.shape)
q = np.sqrt((XX-np.mean(Y)) ** 2 + (YY-np.mean(Y)) ** 2)
beta = k * m_aperture_diameter * q / 2 / L
AiryDisk = Io * (2 * j1(beta) / beta) ** 2
AiryDisk_normalized = AiryDisk/AiryDisk.max()
deblurredROI_wiener = wiener(ROI_reg_norm, psf=AiryDisk, balance=7)
deblurredROI = deblurredROI_wiener
deblurredROI = deblurredROI / deblurredROI.max() * 255.0
enhancedFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] = np.abs(deblurredROI)
ROI_enhanced_arr.append(deblurredROI)
enhancedFrames.append(enhancedFrame)
frame1 = ImageTk.PhotoImage(
Image.fromarray(ROI_arr[i].astype(np.uint8)))
L1['image'] = frame1
window.update()
frame2 = ImageTk.PhotoImage(Image.fromarray(
ROI_enhanced_arr[i].astype(np.uint8)))
L2['image'] = frame2
window.update()
if cv2.waitKey(25) & 0xFF == ord('q'):
break
i += 1
cv2.destroyAllWindows()
def endeturbulence_old_version():
source_file = browseFiles()
dataType = np.float32
N_FirstReference = 10
L = 11
patch_size = (L, L)
patch_half_size = (
int((patch_size[0] - 1) / 2), int((patch_size[1] - 1) / 2))
patches_shift = 1
registration_interval = (15, 15)
R = 0.08
m_lambda0 = 0.55 * 10 ** -6
m_aperture = 0.06
m_focal_length = 250 * 10 ** -3
fno = m_focal_length / m_aperture
readVideo = 1
ReferenceInitializationOpt = 2
video_path = source_file
ImagesSequence = loadVideo(video_path)
# ImagesSequence = loadVideo()
ImagesSequence = np.array(ImagesSequence).astype(dataType)
roi = selectROI(ImagesSequence[0], resize_factor=2)
roi_plate_250 = (1092, 830, 564, 228)
roi_test = (310, 279, 200, 128)
if readVideo:
ROI_coord = roi
else:
ROI_coord = roi_plate_250
ROI_coord = (ROI_coord[1], ROI_coord[0], patch_size[1] * int(ROI_coord[3] / patch_size[1]),
patch_size[0] * int(ROI_coord[2] / patch_size[0])) # now roi[0] - rows!
ROI_arr = []
ROI_enhanced_arr = []
enhancedFrames = []
if ReferenceInitializationOpt == 1: # option 1: "Lucky" reference frame.
FusedPatch = MaxSharpnessFusedPatch([frame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
for frame in ImagesSequence[:N_FirstReference]], patch_half_size)
ReferenceFrame = ImagesSequence[N_FirstReference]
ReferenceFrame[ROI_coord[0] + patch_half_size[0]:ROI_coord[0] + ROI_coord[2] - patch_half_size[0],
ROI_coord[1] + patch_half_size[1]:ROI_coord[1] + ROI_coord[3] - patch_half_size[1]] = FusedPatch
startRegistrationFrame = N_FirstReference
# option 2: Mean of N_FirstReference frames.
elif ReferenceInitializationOpt == 2:
ReferenceFrame = np.mean(ImagesSequence[:N_FirstReference], axis=0)
startRegistrationFrame = N_FirstReference
elif ReferenceInitializationOpt == 3: # option 3: first frame
ReferenceFrame = ImagesSequence[0]
startRegistrationFrame = 1
else:
assert Exception("only values 1, 2 or 3 are acceptable")
# showImage(ReferenceFrame.astype(np.uint8))
enhancedFrames.append(ReferenceFrame)
i = 0
for frame in ImagesSequence[startRegistrationFrame:]:
t = time.time()
enhancedFrame = np.copy(frame)
ROI = frame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
ROI_arr.append(ROI*255.0/ROI.max())
# Image Registration via optical flow
no_rows_Cropped_Frame, no_cols_Cropped_Frame = \
(ROI_coord[2] + 2 * registration_interval[0],
ROI_coord[3] + 2 * registration_interval[1])
u, v = optical_flow_tvl1(
ReferenceFrame[ROI_coord[0] - registration_interval[0]:ROI_coord[0] + ROI_coord[2] + registration_interval[0],
ROI_coord[1] - registration_interval[1]:ROI_coord[1] + ROI_coord[3] + registration_interval[1]],
enhancedFrame[ROI_coord[0] - registration_interval[0]:ROI_coord[0] + ROI_coord[2] + registration_interval[0],
ROI_coord[1] - registration_interval[1]:ROI_coord[1] + ROI_coord[3] + registration_interval[1]],
attachment=10, tightness=0.3, num_warp=3, num_iter=5, tol=4e-4, prefilter=False)
row_coords, col_coords = np.meshgrid(np.arange(no_rows_Cropped_Frame), np.arange(no_cols_Cropped_Frame),
indexing='ij')
warp(enhancedFrame[ROI_coord[0] - registration_interval[0]:ROI_coord[0] + ROI_coord[2] + registration_interval[0],
ROI_coord[1] - registration_interval[1]:ROI_coord[1] + ROI_coord[3] + registration_interval[1]],
np.array([row_coords + v, col_coords + u]), mode='nearest', preserve_range=True).astype(dataType)
# Iterative averaging ROI
ReferenceFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] = \
(1 - R) * ReferenceFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] + \
R * frame[ROI_coord[0]: ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
ROI_registered = ReferenceFrame[ROI_coord[0]:ROI_coord[0] +
ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
m_lambda0 = 0.55 * 10 ** -6
m_aperture_diameter = 0.055
m_focal_length = 250 * 10 ** -3
fno = m_focal_length / m_aperture_diameter
ROI_reg_norm = ROI_registered / 255
k = (2 * np.pi) / m_lambda0 # wavenumber of light in vacuum
Io = 1.0 # relative intensity
L = 250 # distance of screen from aperture
X = np.arange(-m_aperture_diameter/2, m_aperture_diameter /
2, m_aperture_diameter/70) # pupil coordinates
Y = X
XX, YY = np.meshgrid(X, Y)
AiryDisk = np.zeros(XX.shape)
q = np.sqrt((XX-np.mean(Y)) ** 2 + (YY-np.mean(Y)) ** 2)
beta = k * m_aperture_diameter * q / 2 / L
AiryDisk = Io * (2 * j1(beta) / beta) ** 2
AiryDisk_normalized = AiryDisk/AiryDisk.max()
deblurredROI_wiener = wiener(ROI_reg_norm, psf=AiryDisk, balance=7)
deblurredROI = deblurredROI_wiener
deblurredROI = deblurredROI / deblurredROI.max() * 255.0
enhancedFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] = np.abs(deblurredROI)
ROI_enhanced_arr.append(deblurredROI)
enhancedFrames.append(enhancedFrame)
print('Frame analysis time: ', time.time() - t)
cv2.imshow('Input', ROI_arr[i].astype(np.uint8))
cv2.imshow('Output', ROI_enhanced_arr[i].astype(np.uint8))
if cv2.waitKey(20) & 0xFF == ord('q'):
break
i += 1
cv2.destroyAllWindows()
def deturbWithObjDetec():
try:
global path
if flag_for_browse:
path = browseFiles()
print("deturbulenceT PATH: ", path)
except:
print("Exception Found")
dataType = np.float32
N_FirstReference = 10
L = 11
patch_size = (L, L) # (y,x) [pixels]. isoplanatic region
patch_half_size = (
int((patch_size[0] - 1) / 2), int((patch_size[1] - 1) / 2))
patches_shift = 1 # when equals to one we get full overlap.
# (y,x). for each side: up/down/left/right
registration_interval = (15, 15)
R = 0.08 # iterativeAverageConstant
m_lambda0 = 0.55 * 10 ** -6
m_aperture = 0.06
m_focal_length = 250 * 10 ** -3
fno = m_focal_length / m_aperture
cap = cv2.VideoCapture(path)
ImagesSequenceList = []
ROI_arr_to_save = []
ROI_enhanced_arr_to_save = []
Combined_frames = []
Combined_frames_to_save = []
itr_fps = 0
start = time.time()
# ImagesSequence = loadVideo(0)
while True:
ret, frame = cap.read()
print(ret)
itr_fps += 1
end = time.time()
if keyboard.is_pressed('q'):
L1.config(image='')
L2.config(image='')
displayVar.set(str(0))
displayVarFAT.set(str(0))
cap.release()
concatenatedVid = [np.hstack((ROI_arr_to_save[i], np.zeros(
(ROI_arr_to_save[0].shape[0], 10)), Combined_frames[i])).astype(np.float32) for i in range(len(ROI_arr_to_save))]
write_video(concatenatedVid, 2, 'combined', True)
return
if ret:
#frame = cv2.flip(frame, 1)
fps = itr_fps/(end-start)
displayVar.set(str(int(fps)))
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
if(len(ImagesSequenceList) > 10):
ImagesSequenceList.clear()
ImagesSequenceList.append(frame)
ImagesSequence = np.array(ImagesSequenceList).astype(dataType)
# roi = selectROI(ImagesSequence[0], resize_factor=2)
roi = (0, 0, ImagesSequence[0].shape[0],
ImagesSequence[0].shape[1])
ROI_coord = roi
ROI_coord = (ROI_coord[1], ROI_coord[0], patch_size[1] * int(ROI_coord[3] / patch_size[1]),
patch_size[0] * int(ROI_coord[2] / patch_size[0])) # now roi[0] - rows!
ROI_arr = []
ROI_enhanced_arr = []
enhancedFrames = []
# option 2: Mean of N_FirstReference frames.
ReferenceFrame = np.mean(ImagesSequence[:N_FirstReference], axis=0)
startRegistrationFrame = N_FirstReference
enhancedFrames.append(ReferenceFrame)
i = 0
# print("LEN OF IMAGES SEQUENCE : ", len(ImagesSequence))
if len(ImagesSequenceList) > 10:
for frame in ImagesSequence[startRegistrationFrame:]:
t = time.time()
enhancedFrame = np.copy(frame)
ROI = frame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
ROI_arr.append(ROI*255.0/ROI.max())
ROI_arr_to_save.append(ROI*255.0/ROI.max())
no_rows_Cropped_Frame, no_cols_Cropped_Frame = \
(ROI_coord[2] + 2 * registration_interval[0],
ROI_coord[3] + 2 * registration_interval[1])
ReferenceFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] = \
(1 - R) * ReferenceFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] + \
R * frame[ROI_coord[0]: ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
ROI_registered = ReferenceFrame[ROI_coord[0]:ROI_coord[0] +
ROI_coord[2], ROI_coord[1]:ROI_coord[1] + ROI_coord[3]]
m_lambda0 = 0.55 * 10 ** -6
m_aperture_diameter = 0.055
m_focal_length = 250 * 10 ** -3
fno = m_focal_length / m_aperture_diameter
ROI_reg_norm = ROI_registered / 255
k = (2 * np.pi) / m_lambda0
Io = 1.0
L = 250
X = np.arange(-m_aperture_diameter/2,
m_aperture_diameter/2, m_aperture_diameter/70)
Y = X
XX, YY = np.meshgrid(X, Y)
AiryDisk = np.zeros(XX.shape)
q = np.sqrt((XX-np.mean(Y)) ** 2 + (YY-np.mean(Y)) ** 2)
beta = k * m_aperture_diameter * q / 2 / L
AiryDisk = Io * (2 * j1(beta) / beta) ** 2
AiryDisk_normalized = AiryDisk/AiryDisk.max()
deblurredROI_wiener = wiener(
ROI_reg_norm, psf=AiryDisk, balance=7)
deblurredROI = deblurredROI_wiener
deblurredROI = deblurredROI / deblurredROI.max() * 255.0
enhancedFrame[ROI_coord[0]:ROI_coord[0] + ROI_coord[2],
ROI_coord[1]:ROI_coord[1] + ROI_coord[3]] = np.abs(deblurredROI)
ROI_enhanced_arr.clear()
ROI_enhanced_arr.append(deblurredROI)
ROI_enhanced_arr_to_save.append(deblurredROI)
enhancedFrames.append(enhancedFrame)
print('Frame analysis time: ', time.time() - t)
displayVarFAT.set(str("{:.3f}".format(time.time() - t)))
try:
inp_roi = ImageTk.PhotoImage(
Image.fromarray(ROI_arr[i].astype(np.uint8)))
out_roi = ImageTk.PhotoImage(Image.fromarray(
ROI_enhanced_arr[i].astype(np.uint8)))
except:
L1.config(image='')
L2.config(image='')
return
gray_oldframe = ROI_arr[i].astype(np.uint8)
if(is_blur):
gray_oldframe = GaussianBlur(
gray_oldframe, kernel_gauss, 0)
oldBlurMatrix = np.float32(gray_oldframe)
accumulateWeighted(gray_oldframe, oldBlurMatrix, 0.003)
# frame = cv2.flip(ROI_enhanced_arr[i].astype(np.uint8), 1)
frame = ROI_enhanced_arr[i].astype(np.uint8)
gray_frame = frame
if(is_blur):
newBlur_frame = GaussianBlur(
gray_frame, kernel_gauss, 0)
else:
newBlur_frame = gray_frame
newBlurMatrix = np.float32(newBlur_frame)
minusMatrix = absdiff(newBlurMatrix, oldBlurMatrix)
ret, minus_frame = threshold(
minusMatrix, 60, 255.0, THRESH_BINARY)
accumulateWeighted(newBlurMatrix, oldBlurMatrix, 0.02)
if(is_blur):
minus_frame = GaussianBlur(
minus_frame, kernel_gauss, 0)
minus_Matrix = np.float32(minus_frame)
if(is_close):
for itr in range(get_current_value1()):
minus_Matrix = dilate(minus_Matrix, kernel_d)
for itr in range(get_current_value2()):
minus_Matrix = erode(minus_Matrix, kernel_e)
minus_Matrix = np.clip(minus_Matrix, 0, 255)
minus_Matrix = np.array(minus_Matrix, np.uint8)
contours, hierarchy = findContours(
minus_Matrix.copy(), RETR_TREE, CHAIN_APPROX_SIMPLE)
for c in contours:
(x, y, w, h) = boundingRect(c)
rectangle(frame, (x, y), (x + w, y + h),
(0, 255, 0), 2)
if(is_draw_ct):
drawContours(frame, contours, -1, (0, 255, 255), 2)
# frame = cv2.flip(frame, 1)
Combined_frames.append(frame)
out_frame = ImageTk.PhotoImage(Image.fromarray(frame))
L1['image'] = inp_roi
L2['image'] = out_frame
window.update()
i += 1
# concatenatedVid = [np.hstack((ROI_arr[i], np.zeros(
# (ROI_arr[0].shape[0], 10)), Combined_frames[i])).astype(np.float32) for i in range(len(ROI_arr))]
# write_video(concatenatedVid, 10, 'combined', True)
cv2.destroyAllWindows()
# ____________Receiving Input from User_________________
open_popup()
# _____________________CREATING BUTTONS______________________
C3 = Button(window, text="Object Detection", font=(
"Times New Roman", 12, 'bold'), command=lambda: [toggleCapture(), objdetect()]).place(x=880, y=10)
C4 = Button(window, text="Turbulence Mitigation", font=(
"Times New Roman", 12, 'bold'), command=deturbulence).place(x=1090, y=10)
C5 = Button(window, text="Enhanced - TM", font=("Times New Roman",
12, 'bold'), command=endeturbulence_old_version).place(x=1090, y=60)
C6 = Button(window, text="TM + Detection", font=("Times New Roman",
12, 'bold'), command=deturbWithObjDetec).place(x=1280, y=10)
C7 = Button(window, text="Switch Input", font=("Times New Roman",
12, 'bold'), command=open_popup).place(x=1280, y=60)
window.state('zoomed')
window.mainloop()
# ____________________END OF PROGRAM______________________