Sunil Sarolkar commited on
Commit
65c0b8a
·
1 Parent(s): 82a7886

handled closure of opened file

Browse files
Files changed (1) hide show
  1. app.py +246 -147
app.py CHANGED
@@ -394,53 +394,85 @@ if app_mode =='About App':
394
 
395
  ''')
396
 
397
- elif app_mode == 'Run on Test Videos':
398
- category = st.sidebar.selectbox(
399
- 'Choose Category',
400
- np.sort(test_files_df['Category'].unique(), axis=-1, kind='mergesort')
401
- )
402
- mask = (test_files_df['Category'] == category)
403
- test_files_df_category = test_files_df[mask]
404
-
405
- cls = st.sidebar.selectbox(
406
- 'Choose Class',
407
  np.sort(test_files_df_category['Class'].unique(), axis=-1, kind='mergesort')
408
  )
409
- mask = (test_files_df['Class'] == cls)
410
- filename = st.sidebar.selectbox(
411
- 'Choose File',
412
  np.sort(test_files_df_category[mask]['Filename'].unique(), axis=-1, kind='mergesort')
413
  )
414
-
 
 
 
415
  if st.sidebar.button("Start", type="primary"):
416
- # reset state for fresh run
417
- frame_wise_outputs = {}
418
 
419
- mask = (
420
- (testing_df['FileName'] == filename) &
421
- (testing_df['Type'] == category) &
422
- (testing_df['Expression'] == cls)
423
- )
424
- current_test_df = testing_df[mask]
425
 
426
- window_size = 20
427
- X_test_filtered, y_test_filtered = create_timeseries_data(
428
- current_test_df, feature_columns_new, label_columns, window_size=window_size
429
- )
430
- X_test_filtered = np.array(X_test_filtered)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431
 
432
  st.sidebar.markdown('---')
433
- st.markdown(" ## Output")
 
434
 
435
  runtime_progress = st.empty()
 
436
  with runtime_progress.container():
437
- df1 = pd.DataFrame([['--', '--']], columns=['Frames Processed', 'Detected Class'])
 
438
  my_table = st.table(df1)
 
439
 
 
 
 
 
 
 
 
 
440
  view = st.empty()
 
441
  st.markdown("<hr/>", unsafe_allow_html=True)
 
 
 
442
 
443
- # ✅ download video
444
  vid_file = hf_hub_download(
445
  repo_id="sunilsarolkar/isl-test-data",
446
  filename=f'test/{category}/{cls}/{filename}',
@@ -448,120 +480,187 @@ elif app_mode == 'Run on Test Videos':
448
  )
449
 
450
  vid = cv2.VideoCapture(vid_file)
451
- if not vid.isOpened():
452
- st.error(f"Could not open video: {vid_file}")
453
- else:
454
- # parse video metadata
455
- ffprobe_result = ffprobe(vid_file)
456
- info = json.loads(ffprobe_result.json)
457
- videoinfo = [i for i in info["streams"] if i["codec_type"] == "video"][0]
458
- input_fps = videoinfo["avg_frame_rate"]
459
- input_pix_fmt = videoinfo["pix_fmt"]
460
- input_vcodec = videoinfo["codec_name"]
461
- postfix = info["format"]["format_name"].split(",")[0]
462
-
463
- totalFrames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
464
- st.write(f"Opened video with {totalFrames} frames")
465
-
466
- output_file = f"/tmp/output_{uuid.uuid4().hex}.{postfix}"
467
- writer = None
468
- window = []
469
- weighted_avg_dict = {}
470
- idx = 0
471
- prevTime = 0
472
-
473
- try:
474
- for _, row in current_test_df.iterrows():
475
- ret, frame = vid.read()
476
- if not ret:
477
- break
478
-
479
- if len(window) < window_size:
480
- # burn-in period (first 20 frames)
481
- canvas = util.drawStickmodel(
482
- frame,
483
- eval(row['bodypose_circles']),
484
- eval(row['bodypose_sticks']),
485
- eval(row['handpose_edges']),
486
- eval(row['handpose_peaks'])
487
- )
488
- canvas_with_plot = util.draw_bar_plot_below_image(
489
- canvas, {}, f'Prediction bar plot - Frame {idx+1} [no predictions]', canvas
490
- )
491
- canvas_with_plot = util.add_padding_to_bottom(canvas_with_plot, (255,255,255), 100)
492
-
493
- if writer is None:
494
- input_framesize = canvas_with_plot.shape[:2]
495
- writer = Writer(output_file, input_fps, input_framesize, input_pix_fmt, input_vcodec)
496
-
497
- writer(canvas_with_plot)
498
- with runtime_progress.container():
499
- df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}', '<model will output after 20 frames>']],
500
- columns=['Frames Processed', 'Detected Class'])
501
- my_table = st.table(df1)
502
- window.append(frame)
503
- with view.container():
504
- st.image(canvas_with_plot, channels='BGR', use_column_width=True)
505
-
506
- else:
507
- # inference after burn-in
508
- window[:-1] = window[1:]
509
- window[-1] = frame
510
-
511
- translation_model = get_translator_model()
512
- encoded_translation = translation_model(
513
- X_test_filtered[idx-20].reshape(1, X_test_filtered[idx-20].shape[0], X_test_filtered[idx-20].shape[1])
514
- )
515
- encoded_translation = encoded_translation[0].cpu().detach().numpy()
516
- top_3_probs = encoded_translation.argsort()[-3:][::-1]
517
- top_3_categories = [expression_mapping[i] for i in top_3_probs]
518
- top_3_values = encoded_translation[top_3_probs]
519
-
520
- for category, prob in zip(top_3_categories, top_3_values):
521
- frame_wise_outputs.setdefault(category, []).append(prob)
522
-
523
- current_prob = dict(zip(top_3_categories, top_3_values))
524
- for key, values in frame_wise_outputs.items():
525
- weighted_avg_dict[key] = weighted_average(values, [len(values)]*len(values))
526
-
527
- canvas = util.drawStickmodel(
528
- frame,
529
- eval(row['bodypose_circles']),
530
- eval(row['bodypose_sticks']),
531
- eval(row['handpose_edges']),
532
- eval(row['handpose_peaks'])
533
- )
534
- canvas_with_plot = util.draw_bar_plot_below_image(
535
- canvas, current_prob, f'Prediction at window({idx-20+1}-{idx+1})', canvas
536
- )
537
- canvas_with_plot = util.draw_bar_plot_below_image(
538
- canvas_with_plot, weighted_avg_dict, f'Weighted avg till frame {idx+1}', canvas
539
- )
540
- canvas_with_plot = util.add_padding_to_bottom(canvas_with_plot, (255,255,255), 100)
541
- writer(canvas_with_plot)
542
-
543
- # update display
544
- max_key, max_prob = max(weighted_avg_dict.items(), key=lambda kv: kv[1])
545
- with runtime_progress.container():
546
- df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}',
547
- f'{max_key} ({max_prob*100:.2f}%)']],
548
- columns=['Frames Processed','Detected Class'])
549
- my_table = st.table(df1)
550
- with view.container():
551
- st.image(canvas_with_plot, channels='BGR', use_column_width=True)
552
-
553
- idx += 1
554
-
555
- # after loop
556
- with view.container():
557
- if writer is not None:
558
- writer.close() # block until ffmpeg finishes
559
- with open(output_file, 'rb') as f:
560
- st.video(f.read())
561
- st.success(f"Output saved to {output_file}")
562
- else:
563
- st.warning("No video was processed this run.")
564
-
565
- finally:
566
- vid.release()
567
- cv2.destroyAllWindows()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
 
395
  ''')
396
 
397
+ elif app_mode =='Run on Test Videos':
398
+ # placeholder = st.empty()
399
+ category = st.sidebar.selectbox('Choose Category',
400
+ np.sort(test_files_df['Category'].unique(), axis=-1, kind='mergesort'))
401
+ # print(category)
402
+ mask = (test_files_df['Category']==category)
403
+ test_files_df_category=test_files_df[mask]
404
+ cls = st.sidebar.selectbox('Choose Class',
 
 
405
  np.sort(test_files_df_category['Class'].unique(), axis=-1, kind='mergesort')
406
  )
407
+ mask = (test_files_df['Class']==cls)
408
+ filename = st.sidebar.selectbox('Choose File',
 
409
  np.sort(test_files_df_category[mask]['Filename'].unique(), axis=-1, kind='mergesort')
410
  )
411
+ # print(f'test/{category}/{cls}/{filename}')
412
+ # mask = (include_df['Filepath'].str.contains(key[0])) & (include_df['type']==key[2]) & (include_df['expression']==key[1])
413
+ # stframe = st.empty()
414
+
415
  if st.sidebar.button("Start", type="primary"):
416
+ mask = (testing_df['FileName'] == filename) & (testing_df['Type']==category)& (testing_df['Expression']==cls)
417
+ # filtered_df = current_test_df.sort_
418
 
419
+ window_size=20
420
+ current_test_df=testing_df[mask]
421
+ X_test_filtered,y_test_filtered = create_timeseries_data(current_test_df,feature_columns_new,label_columns,window_size=window_size)
422
+ # y_filtered_encoded=to_categorical(y_test_filtered, num_classes=len(df['Expression_encoded'].unique()))
423
+ X_test_filtered=np.array(X_test_filtered)
 
424
 
425
+ # encoded_translation=model(frame.reshape(1,frame.shape[0],frame.shape[1]))
426
+ st.set_option('deprecation.showfileUploaderEncoding', False)
427
+
428
+ # use_webcam = st.sidebar.button('Use Webcam')
429
+ # record = st.sidebar.checkbox("Record Video")
430
+ # if record:
431
+ # st.checkbox("Recording", value=True)
432
+
433
+ st.sidebar.markdown('---')
434
+ st.markdown(
435
+ """
436
+ <style>
437
+ [data-testid="stSidebar"][aria-expanded="true"] > div:first-child {
438
+ width: 400px;
439
+ }
440
+ [data-testid="stSidebar"][aria-expanded="false"] > div:first-child {
441
+ width: 400px;
442
+ margin-left: -400px;
443
+ }
444
+ </style>
445
+ """,
446
+ unsafe_allow_html=True,
447
+ )
448
 
449
  st.sidebar.markdown('---')
450
+
451
+ st.markdown(' ## Output')
452
 
453
  runtime_progress = st.empty()
454
+
455
  with runtime_progress.container():
456
+ df1 = pd.DataFrame([['--','--']], columns=['Frames Processed','Detected Class'])
457
+
458
  my_table = st.table(df1)
459
+ # kpi1, kpi2 = st.columns(2)
460
 
461
+ # with kpi1:
462
+ # st.markdown("**Frames Processed**")
463
+ # kpi1_text = st.markdown(f'0/{current_test_df.shape[0]}')
464
+
465
+ # with kpi2:
466
+ # st.markdown("**Detected Class**")
467
+ # kpi2_text = st.markdown("--")
468
+
469
  view = st.empty()
470
+
471
  st.markdown("<hr/>", unsafe_allow_html=True)
472
+ stframes = st.empty()#[st.empty() for _ in range(20)]
473
+ # video_file_buffer = st.sidebar.file_uploader("Upload a video", type=[ "mp4", "mov",'avi','asf', 'm4v' ])
474
+ # tfflie = tempfile.NamedTemporaryFile(delete=False)
475
 
 
476
  vid_file = hf_hub_download(
477
  repo_id="sunilsarolkar/isl-test-data",
478
  filename=f'test/{category}/{cls}/{filename}',
 
480
  )
481
 
482
  vid = cv2.VideoCapture(vid_file)
483
+
484
+ ffprobe_result = ffprobe(vid_file)
485
+ info = json.loads(ffprobe_result.json)
486
+ videoinfo = [i for i in info["streams"] if i["codec_type"] == "video"][0]
487
+ input_fps = videoinfo["avg_frame_rate"]
488
+ # input_fps = float(input_fps[0])/float(input_fps[1])
489
+ input_pix_fmt = videoinfo["pix_fmt"]
490
+ input_vcodec = videoinfo["codec_name"]
491
+ postfix = info["format"]["format_name"].split(",")[0]
492
+ # print(f'input_vcodec-{input_vcodec}')
493
+
494
+ width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
495
+ height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
496
+ fps_input = int(vid.get(cv2.CAP_PROP_FPS))
497
+
498
+ #codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
499
+ # codec = cv2.VideoWriter_fourcc('V','P','0','9')
500
+ # out = cv2.VideoWriter('output1.mp4', codec, fps_input, (width, height))
501
+
502
+ # st.sidebar.text('Input Video')
503
+ # st.sidebar.video(tfflie.name)
504
+ fps = 0
505
+ i = 0
506
+
507
+
508
+
509
+ # cap = cv2.VideoCapture(video_file,)
510
+ totalFrames=int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
511
+ window_size=20
512
+ # print('current_test_df',current_test_df)
513
+ # print('totalFrames',totalFrames)
514
+ window=[]
515
+
516
+
517
+ prevTime = 0
518
+ postfix = info["format"]["format_name"].split(",")[0]
519
+
520
+
521
+ output_file = f"/tmp/output_{uuid.uuid4().hex}.{postfix}"
522
+
523
+ # height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
524
+ fps_input = int(vid.get(cv2.CAP_PROP_FPS))
525
+
526
+ #codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
527
+
528
+ # codec = cv2.VideoWriter_fourcc('m','p','4','v')
529
+ out = None
530
+ writer=None
531
+ weighted_avg_dict={}
532
+
533
+ idx=0
534
+ try:
535
+ for _, row in current_test_df.iterrows():#enumerate(file_df.rolling(window=20, step=20,min_periods=1)):
536
+ # print(f'captured frame#{idx}')
537
+ if not vid.isOpened():
538
+ st.error(f"Could not open video: {vid_file}")
539
+ else:
540
+ if(vid.isOpened()):
541
+ ret, frame = vid.read()
542
+
543
+
544
+ if len(window)<window_size:
545
+ canvas=util.drawStickmodel(frame,eval(row['bodypose_circles']),eval(row['bodypose_sticks']),eval(row['handpose_edges']),eval(row['handpose_peaks']))
546
+ canvas_with_plot=util.draw_bar_plot_below_image(canvas,{}, f'Prediction bar plot - Frame number {idx+1} [** no predictions]',canvas)
547
+ canvas_with_plot=util.draw_bar_plot_below_image(canvas_with_plot,weighted_avg_dict, f'Weighted avg - Frame number {idx+1} [** no predictions]',canvas)
548
+ canvas_with_plot=util.add_padding_to_bottom(canvas_with_plot,(255,255,255),100)# Adds padding at bottom
549
+
550
+ if writer is None:
551
+ input_framesize = canvas_with_plot.shape[:2]
552
+ writer = Writer(output_file, input_fps, input_framesize, input_pix_fmt,
553
+ input_vcodec)
554
+
555
+ # if out is None:
556
+ # out=cv2.VideoWriter(output_file, codec, fps_input, frame.shape[:2])
557
+
558
+
559
+ writer(canvas_with_plot)
560
+ # out.write(canvas)
561
+ with runtime_progress.container():
562
+ df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}','<model will output after 20 frames>']], columns=['Frames Processed','Detected Class'])
563
+
564
+ my_table = st.table(df1)
565
+ window.append(frame)
566
+ # kpi1_text.write(f"<h1 style='text-align: center; color: red;'>{idx+1}/{current_test_df.shape[0]}</h1>", unsafe_allow_html=True)
567
+ # kpi2_text.write(f"<h1 style='text-align: center; color: red;'>--</h1>", unsafe_allow_html=True)
568
+ with view.container():
569
+ st.image(canvas_with_plot,channels = 'BGR',use_column_width=True)
570
+ else:
571
+
572
+ window[:-1] = window[1:]
573
+ window[-1]=frame
574
+ translation_model=get_translator_model()
575
+ # testing_df[]
576
+
577
+ encoded_translation = translation_model(X_test_filtered[idx-20].reshape(1,X_test_filtered[idx-20].shape[0],X_test_filtered[idx-20].shape[1]))
578
+ encoded_translation=encoded_translation[0].cpu().detach().numpy()
579
+ sorted_index=np.argsort(encoded_translation)[::-1]
580
+ maxindex=np.argmax(encoded_translation)
581
+
582
+ top_3_probs = encoded_translation.argsort()[-3:][::-1] # Get indices of top 3 probabilities (descending order)
583
+ top_3_categories = [expression_mapping[i] for i in top_3_probs] # Convert indices to category names (assuming class_names list exists)
584
+ top_3_values = encoded_translation[top_3_probs] # Get corresponding probabilities
585
+ # print(f'{idx} {encoded_translation[maxindex]:0.4f} {maxindex}-{expression_mapping[maxindex]} ')#{[(pi,encoded_translation[pi],expression_mapping[pi]) for pi in sorted_index]}
586
+ for category, prob in zip(top_3_categories, top_3_values):
587
+ if category not in frame_wise_outputs:
588
+ frame_wise_outputs[category]=[]
589
+ frame_wise_outputs[category].append(prob)
590
+ current_prob={}
591
+
592
+ for category, prob in zip(top_3_categories, top_3_values):
593
+ current_prob[category]=prob
594
+
595
+ for key in frame_wise_outputs:
596
+ weighted_avg_dict[key]=weighted_average(frame_wise_outputs[key],[len(frame_wise_outputs[key]) for i in range(len(frame_wise_outputs[key]))])
597
+
598
+ sorted_dict = dict(sorted(weighted_avg_dict.items(), key=lambda item: item[1], reverse=True))
599
+ canvas=util.drawStickmodel(frame,eval(row['bodypose_circles']),eval(row['bodypose_sticks']),eval(row['handpose_edges']),eval(row['handpose_peaks']))
600
+ canvas_with_plot=util.draw_bar_plot_below_image(canvas,current_prob, f'Prediction at frame window({idx-20+1}-{idx+1})',canvas)
601
+ canvas_with_plot=util.draw_bar_plot_below_image(canvas_with_plot,weighted_avg_dict, f'Weighted avg till window {idx+1}',canvas)
602
+ canvas_with_plot=util.add_padding_to_bottom(canvas_with_plot,(255,255,255),100)
603
+ writer(canvas_with_plot)
604
+
605
+
606
+ currTime = time.time()
607
+ fps = 1 / (currTime - prevTime)
608
+ prevTime = currTime
609
+ # out.write(frame)
610
+ # if record:
611
+ # #st.checkbox("Recording", value=True)
612
+ # out.write(frame)
613
+ #Dashboard
614
+
615
+ max_prob = float('-inf') # Initialize with negative infinity
616
+ max_key = None
617
+
618
+ for exp, prob in weighted_avg_dict.items():
619
+ if prob > max_prob:
620
+ max_prob = prob
621
+ max_key = exp
622
+ with runtime_progress.container():
623
+ df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}',f'{max_key} ({max_prob*100:.2f}%)']], columns=['Frames Processed','Detected Class'])
624
+ my_table = st.table(df1)
625
+ # kpi1_text.write(f"<h1 style='text-align: center; color: red;'>{idx+1}/{current_test_df.shape[0]}</h1>", unsafe_allow_html=True)
626
+ # kpi2_text.write(f"<h1 style='text-align: center; color: red;'>{max_key} ({max_prob*100:.2f}%)</h1>", unsafe_allow_html=True)
627
+ # with placeholder.container():
628
+ # # st.write(weighted_avg_dict)
629
+ # # data = {
630
+ # # "I": 0.7350964583456516,
631
+ # # "Hello": 0.1078806109726429,
632
+ # # "you": 0.11776176246348768,
633
+ # # "you (plural)": 0.12685142129916568
634
+ # # }
635
+
636
+ # # Convert the dictionary to a Pandas DataFrame for easier plotting
637
+ # df = pd.DataFrame.from_dict(weighted_avg_dict, orient='index', columns=['Values'])
638
+
639
+ # # Create a bar chart with Streamlit
640
+ # st.bar_chart(df)
641
+ # frame = cv2.resize(frame,(0,0),fx = 0.8 , fy = 0.8)
642
+ # frame = image_resize(image = frame, width = 640)
643
+ with view.container():
644
+ st.image(canvas_with_plot,channels = 'BGR',use_column_width=True)
645
+
646
+ idx=idx+1
647
+
648
+
649
+ # st.text('Video Processed')
650
+ with view.container():
651
+ if writer is not None: # ✅ safeguard
652
+ writer.close()
653
+ output_video = open(output_file,'rb')
654
+ out_bytes = output_video.read()
655
+ st.video(out_bytes)
656
+ print(f'Output file - {output_file}')
657
+ else:
658
+ st.warning("No video was processed, writer is empty.")
659
+ # out.release()
660
+
661
+ print(f'Output file - {output_file}')
662
+ finally:
663
+ vid.release()
664
+ if writer is not None:
665
+ writer.close()
666
+ cv2.destroyAllWindows()