Sunil Sarolkar
commited on
Commit
·
0252a3f
1
Parent(s):
ef868df
handled closure of opened file
Browse files
app.py
CHANGED
|
@@ -531,130 +531,136 @@ elif app_mode =='Run on Test Videos':
|
|
| 531 |
weighted_avg_dict={}
|
| 532 |
|
| 533 |
idx=0
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
if len(window)<window_size:
|
| 542 |
-
canvas=util.drawStickmodel(frame,eval(row['bodypose_circles']),eval(row['bodypose_sticks']),eval(row['handpose_edges']),eval(row['handpose_peaks']))
|
| 543 |
-
canvas_with_plot=util.draw_bar_plot_below_image(canvas,{}, f'Prediction bar plot - Frame number {idx+1} [** no predictions]',canvas)
|
| 544 |
-
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)
|
| 545 |
-
canvas_with_plot=util.add_padding_to_bottom(canvas_with_plot,(255,255,255),100)# Adds padding at bottom
|
| 546 |
-
|
| 547 |
-
if writer is None:
|
| 548 |
-
input_framesize = canvas_with_plot.shape[:2]
|
| 549 |
-
writer = Writer(output_file, input_fps, input_framesize, input_pix_fmt,
|
| 550 |
-
input_vcodec)
|
| 551 |
-
|
| 552 |
-
# if out is None:
|
| 553 |
-
# out=cv2.VideoWriter(output_file, codec, fps_input, frame.shape[:2])
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
writer(canvas_with_plot)
|
| 557 |
-
# out.write(canvas)
|
| 558 |
-
with runtime_progress.container():
|
| 559 |
-
df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}','<model will output after 20 frames>']], columns=['Frames Processed','Detected Class'])
|
| 560 |
-
|
| 561 |
-
my_table = st.table(df1)
|
| 562 |
-
window.append(frame)
|
| 563 |
-
# kpi1_text.write(f"<h1 style='text-align: center; color: red;'>{idx+1}/{current_test_df.shape[0]}</h1>", unsafe_allow_html=True)
|
| 564 |
-
# kpi2_text.write(f"<h1 style='text-align: center; color: red;'>--</h1>", unsafe_allow_html=True)
|
| 565 |
-
with view.container():
|
| 566 |
-
st.image(canvas_with_plot,channels = 'BGR',use_column_width=True)
|
| 567 |
else:
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
|
| 574 |
-
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]))
|
| 575 |
-
encoded_translation=encoded_translation[0].cpu().detach().numpy()
|
| 576 |
-
sorted_index=np.argsort(encoded_translation)[::-1]
|
| 577 |
-
maxindex=np.argmax(encoded_translation)
|
| 578 |
-
|
| 579 |
-
top_3_probs = encoded_translation.argsort()[-3:][::-1] # Get indices of top 3 probabilities (descending order)
|
| 580 |
-
top_3_categories = [expression_mapping[i] for i in top_3_probs] # Convert indices to category names (assuming class_names list exists)
|
| 581 |
-
top_3_values = encoded_translation[top_3_probs] # Get corresponding probabilities
|
| 582 |
-
# print(f'{idx} {encoded_translation[maxindex]:0.4f} {maxindex}-{expression_mapping[maxindex]} ')#{[(pi,encoded_translation[pi],expression_mapping[pi]) for pi in sorted_index]}
|
| 583 |
-
for category, prob in zip(top_3_categories, top_3_values):
|
| 584 |
-
if category not in frame_wise_outputs:
|
| 585 |
-
frame_wise_outputs[category]=[]
|
| 586 |
-
frame_wise_outputs[category].append(prob)
|
| 587 |
-
current_prob={}
|
| 588 |
-
|
| 589 |
-
for category, prob in zip(top_3_categories, top_3_values):
|
| 590 |
-
current_prob[category]=prob
|
| 591 |
-
|
| 592 |
-
for key in frame_wise_outputs:
|
| 593 |
-
weighted_avg_dict[key]=weighted_average(frame_wise_outputs[key],[len(frame_wise_outputs[key]) for i in range(len(frame_wise_outputs[key]))])
|
| 594 |
-
|
| 595 |
-
sorted_dict = dict(sorted(weighted_avg_dict.items(), key=lambda item: item[1], reverse=True))
|
| 596 |
-
canvas=util.drawStickmodel(frame,eval(row['bodypose_circles']),eval(row['bodypose_sticks']),eval(row['handpose_edges']),eval(row['handpose_peaks']))
|
| 597 |
-
canvas_with_plot=util.draw_bar_plot_below_image(canvas,current_prob, f'Prediction at frame window({idx-20+1}-{idx+1})',canvas)
|
| 598 |
-
canvas_with_plot=util.draw_bar_plot_below_image(canvas_with_plot,weighted_avg_dict, f'Weighted avg till window {idx+1}',canvas)
|
| 599 |
-
canvas_with_plot=util.add_padding_to_bottom(canvas_with_plot,(255,255,255),100)
|
| 600 |
-
writer(canvas_with_plot)
|
| 601 |
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
max_key = None
|
| 614 |
-
|
| 615 |
-
for exp, prob in weighted_avg_dict.items():
|
| 616 |
-
if prob > max_prob:
|
| 617 |
-
max_prob = prob
|
| 618 |
-
max_key = exp
|
| 619 |
-
with runtime_progress.container():
|
| 620 |
-
df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}',f'{max_key} ({max_prob*100:.2f}%)']], columns=['Frames Processed','Detected Class'])
|
| 621 |
-
my_table = st.table(df1)
|
| 622 |
-
# kpi1_text.write(f"<h1 style='text-align: center; color: red;'>{idx+1}/{current_test_df.shape[0]}</h1>", unsafe_allow_html=True)
|
| 623 |
-
# kpi2_text.write(f"<h1 style='text-align: center; color: red;'>{max_key} ({max_prob*100:.2f}%)</h1>", unsafe_allow_html=True)
|
| 624 |
-
# with placeholder.container():
|
| 625 |
-
# # st.write(weighted_avg_dict)
|
| 626 |
-
# # data = {
|
| 627 |
-
# # "I": 0.7350964583456516,
|
| 628 |
-
# # "Hello": 0.1078806109726429,
|
| 629 |
-
# # "you": 0.11776176246348768,
|
| 630 |
-
# # "you (plural)": 0.12685142129916568
|
| 631 |
-
# # }
|
| 632 |
-
|
| 633 |
-
# # Convert the dictionary to a Pandas DataFrame for easier plotting
|
| 634 |
-
# df = pd.DataFrame.from_dict(weighted_avg_dict, orient='index', columns=['Values'])
|
| 635 |
-
|
| 636 |
-
# # Create a bar chart with Streamlit
|
| 637 |
-
# st.bar_chart(df)
|
| 638 |
-
# frame = cv2.resize(frame,(0,0),fx = 0.8 , fy = 0.8)
|
| 639 |
-
# frame = image_resize(image = frame, width = 640)
|
| 640 |
-
with view.container():
|
| 641 |
-
st.image(canvas_with_plot,channels = 'BGR',use_column_width=True)
|
| 642 |
-
|
| 643 |
-
idx=idx+1
|
| 644 |
|
| 645 |
-
|
| 646 |
-
# st.text('Video Processed')
|
| 647 |
-
with view.container():
|
| 648 |
-
if writer is not None: # ✅ safeguard
|
| 649 |
-
writer.close()
|
| 650 |
-
output_video = open(output_file,'rb')
|
| 651 |
-
out_bytes = output_video.read()
|
| 652 |
-
st.video(out_bytes)
|
| 653 |
print(f'Output file - {output_file}')
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
cv2.destroyAllWindows()
|
| 660 |
-
vid.release()
|
|
|
|
| 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()
|
|
|
|
|
|