|
import os |
|
import sys |
|
import cv2 |
|
import os |
|
import glob |
|
import shutil |
|
import gdown |
|
import zipfile |
|
|
|
import time |
|
import random |
|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
from pathlib import Path |
|
sys.path.insert(1, "MEMTrack/src") |
|
from data_prep_utils import process_data |
|
from data_feature_gen import create_train_data, create_test_data |
|
from inferenceBacteriaRetinanet_Motility_v2 import run_inference |
|
from GenerateTrackingData import gen_tracking_data |
|
from Tracking import track_bacteria |
|
from TrackingAnalysis import analyse_tracking |
|
from GenerateVideo import gen_tracking_video |
|
|
|
def find_and_return_csv_files(folder_path, search_pattern): |
|
search_pattern = f"{folder_path}/{search_pattern}*.csv" |
|
csv_files = list(glob.glob(search_pattern)) |
|
return csv_files |
|
|
|
def read_video(video, raw_frame_dir, progress=gr.Progress()): |
|
|
|
video_dir = str(random.randint(111111111, 999999999)) |
|
images_dir = "Images without Labels" |
|
frames_dir = os.path.join(raw_frame_dir, video_dir, images_dir) |
|
os.makedirs(frames_dir, exist_ok=True) |
|
count = 0 |
|
frames = [] |
|
|
|
cap = cv2.VideoCapture(video) |
|
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
processed_frames = 0 |
|
|
|
while cap.isOpened(): |
|
ret, frame = cap.read() |
|
|
|
if ret is False: |
|
break |
|
|
|
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
|
frame_path = os.path.join(frames_dir, f"{count}.jpg") |
|
cv2.imwrite(frame_path, frame) |
|
frames.append(frame) |
|
count += 1 |
|
processed_frames += 1 |
|
print(f"Processing frame {processed_frames}") |
|
progress(processed_frames / total_frames, desc=f"Reading frame {processed_frames}/{total_frames}") |
|
|
|
|
|
cap.release() |
|
return video_dir |
|
|
|
def download_and_unzip_google_drive_file(file_id, output_path, unzip_path): |
|
url = f'https://drive.google.com/uc?id={file_id}' |
|
url="https://drive.usercontent.google.com/download?id=1agsLD5HV_VmDNpDhjHXTCAVmGUm2IQ6p&export=download&&confirm=t" |
|
gdown.download(url, output_path, quiet=False, ) |
|
|
|
with zipfile.ZipFile(output_path, 'r') as zip_ref: |
|
zip_ref.extractall(unzip_path) |
|
|
|
|
|
|
|
def doo(video, progress=gr.Progress()): |
|
|
|
file_id = '1agsLD5HV_VmDNpDhjHXTCAVmGUm2IQ6p' |
|
output_path = 'models.zip' |
|
unzip_path = './' |
|
|
|
download_and_unzip_google_drive_file(file_id, output_path, unzip_path) |
|
|
|
|
|
raw_frame_dir = "raw_data/" |
|
final_data_dir = "data" |
|
out_sub_dir = "bacteria" |
|
target_data_sub_dir = os.path.join(final_data_dir, out_sub_dir) |
|
feature_dir = "DataFeatures" |
|
test_video_list = ["video1"] |
|
exp_name = "collagen_motility_inference" |
|
feature_data_path = os.path.join(feature_dir, exp_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for video_num in [1]: |
|
|
|
|
|
|
|
|
|
folder_path = analyse_tracking(video_num=video_num, data_feature_path=feature_data_path, data_root_path=final_data_dir, plot=True) |
|
progress(3 / 3, desc=f"Tracking {3}/{3}") |
|
output_video = gen_tracking_video(video_num=video_num, fps=60, data_path=feature_data_path) |
|
final_video = os.path.basename(output_video) |
|
shutil.copy(output_video, final_video) |
|
print(output_video) |
|
print(final_video) |
|
|
|
search_pattern = "TrackedRawData" |
|
tracking_preds = find_and_return_csv_files(folder_path, search_pattern) |
|
|
|
|
|
return final_video, tracking_preds |
|
|
|
|
|
examples = [['./sample_videos/control_4_h264.mp4']] |
|
|
|
title = "🎞️ MEMTrack Bacteria Tracking Video Tool" |
|
description = "Upload a video or selct from example to track. <br><br> If the input video does not play on browser, ensure its in a browser accetable format. Output will be generated iirespective of playback on browser. Refer: https://colab.research.google.com/drive/1U5pX_9iaR_T8knVV7o4ftKdDoGndCdEM?usp=sharing" |
|
|
|
iface = gr.Interface( |
|
fn=doo, |
|
inputs=gr.Video(label="Input Video"), |
|
outputs=[ |
|
gr.Video(label="Tracked Video"), |
|
gr.File(label="CSV Data") |
|
], |
|
examples=examples, |
|
title=title, |
|
description=description |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch(share=True) |