Sunil Sarolkar commited on
Commit
b5ce8e2
·
1 Parent(s): 0252a3f

fixed issue with closed file

Browse files
Files changed (1) hide show
  1. app.py +130 -228
app.py CHANGED
@@ -394,85 +394,53 @@ if app_mode =='About App':
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,187 +448,121 @@ elif app_mode =='Run on Test Videos':
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()
 
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
  )
449
 
450
  vid = cv2.VideoCapture(vid_file)
451
+ if not vid.isOpened():
452
+ st.error(f"Could not open video: {vid_file}")
453
+ return
454
 
455
+ # ✅ parse video metadata
456
  ffprobe_result = ffprobe(vid_file)
457
  info = json.loads(ffprobe_result.json)
458
  videoinfo = [i for i in info["streams"] if i["codec_type"] == "video"][0]
459
  input_fps = videoinfo["avg_frame_rate"]
 
460
  input_pix_fmt = videoinfo["pix_fmt"]
461
  input_vcodec = videoinfo["codec_name"]
462
  postfix = info["format"]["format_name"].split(",")[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463
 
464
+ totalFrames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
465
+ st.write(f"Opened video with {totalFrames} frames")
466
 
467
  output_file = f"/tmp/output_{uuid.uuid4().hex}.{postfix}"
468
+ writer = None
469
+ window = []
470
+ weighted_avg_dict = {}
471
+ idx = 0
472
+ prevTime = 0
473
 
 
 
 
 
 
 
 
 
 
 
 
474
  try:
475
+ for _, row in current_test_df.iterrows():
476
+ ret, frame = vid.read()
477
+ if not ret:
478
+ break
479
+
480
+ if len(window) < window_size:
481
+ # burn-in period (first 20 frames)
482
+ canvas = util.drawStickmodel(
483
+ frame,
484
+ eval(row['bodypose_circles']),
485
+ eval(row['bodypose_sticks']),
486
+ eval(row['handpose_edges']),
487
+ eval(row['handpose_peaks'])
488
+ )
489
+ canvas_with_plot = util.draw_bar_plot_below_image(
490
+ canvas, {}, f'Prediction bar plot - Frame {idx+1} [no predictions]', canvas
491
+ )
492
+ canvas_with_plot = util.add_padding_to_bottom(canvas_with_plot, (255,255,255), 100)
493
+
494
+ if writer is None:
495
+ input_framesize = canvas_with_plot.shape[:2]
496
+ writer = Writer(output_file, input_fps, input_framesize, input_pix_fmt, input_vcodec)
497
+
498
+ writer(canvas_with_plot)
499
+ with runtime_progress.container():
500
+ df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}', '<model will output after 20 frames>']],
501
+ columns=['Frames Processed', 'Detected Class'])
502
+ my_table = st.table(df1)
503
+ window.append(frame)
504
+ with view.container():
505
+ st.image(canvas_with_plot, channels='BGR', use_column_width=True)
506
+
507
  else:
508
+ # inference after burn-in
509
+ window[:-1] = window[1:]
510
+ window[-1] = frame
511
+
512
+ translation_model = get_translator_model()
513
+ encoded_translation = translation_model(
514
+ X_test_filtered[idx-20].reshape(1, X_test_filtered[idx-20].shape[0], X_test_filtered[idx-20].shape[1])
515
+ )
516
+ encoded_translation = encoded_translation[0].cpu().detach().numpy()
517
+ top_3_probs = encoded_translation.argsort()[-3:][::-1]
518
+ top_3_categories = [expression_mapping[i] for i in top_3_probs]
519
+ top_3_values = encoded_translation[top_3_probs]
520
+
521
+ for category, prob in zip(top_3_categories, top_3_values):
522
+ frame_wise_outputs.setdefault(category, []).append(prob)
523
+
524
+ current_prob = dict(zip(top_3_categories, top_3_values))
525
+ for key, values in frame_wise_outputs.items():
526
+ weighted_avg_dict[key] = weighted_average(values, [len(values)]*len(values))
527
+
528
+ canvas = util.drawStickmodel(
529
+ frame,
530
+ eval(row['bodypose_circles']),
531
+ eval(row['bodypose_sticks']),
532
+ eval(row['handpose_edges']),
533
+ eval(row['handpose_peaks'])
534
+ )
535
+ canvas_with_plot = util.draw_bar_plot_below_image(
536
+ canvas, current_prob, f'Prediction at window({idx-20+1}-{idx+1})', canvas
537
+ )
538
+ canvas_with_plot = util.draw_bar_plot_below_image(
539
+ canvas_with_plot, weighted_avg_dict, f'Weighted avg till frame {idx+1}', canvas
540
+ )
541
+ canvas_with_plot = util.add_padding_to_bottom(canvas_with_plot, (255,255,255), 100)
542
+ writer(canvas_with_plot)
543
+
544
+ # update display
545
+ max_key, max_prob = max(weighted_avg_dict.items(), key=lambda kv: kv[1])
546
+ with runtime_progress.container():
547
+ df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}',
548
+ f'{max_key} ({max_prob*100:.2f}%)']],
549
+ columns=['Frames Processed','Detected Class'])
550
+ my_table = st.table(df1)
551
+ with view.container():
552
+ st.image(canvas_with_plot, channels='BGR', use_column_width=True)
553
+
554
+ idx += 1
555
+
556
+ # ✅ after loop
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557
  with view.container():
558
+ if writer is not None:
559
+ writer.close() # block until ffmpeg finishes
560
+ with open(output_file, 'rb') as f:
561
+ st.video(f.read())
562
+ st.success(f"Output saved to {output_file}")
 
563
  else:
564
+ st.warning("No video was processed this run.")
565
+
 
 
566
  finally:
567
  vid.release()
568
+ cv2.destroyAllWindows()