|
BATCH_SIZE = 8 |
|
CUDA_PATH = "/usr/local/cuda-12.3/" |
|
|
|
|
|
from datetime import datetime |
|
import os |
|
import sys |
|
|
|
os.environ["CUDA_HOME"] = CUDA_PATH |
|
|
|
import gradio as gr |
|
from tqdm import tqdm |
|
import os |
|
import time |
|
import shutil |
|
|
|
from owl_batch import owl_batch_video |
|
|
|
|
|
|
|
|
|
|
|
def run_owl_batch( |
|
input_vids : list[str] | str, |
|
target_prompt: str, |
|
species_prompt: str, |
|
conf_threshold: float, |
|
fps_processed: int, |
|
scaling_factor: float, |
|
progress=gr.Progress() |
|
) -> tuple[str, str, str]: |
|
""" |
|
args: |
|
input_vids: list of video paths |
|
target_prompt: prompt to search for |
|
species_prompt: prompt to query |
|
threshold: threshold for detection |
|
fps_processed: number of frames per second to process |
|
scaling_factor: factor to scale the frames by |
|
returns: |
|
csv_path: path to csv file |
|
pos_zip: path to zip file of positive videos |
|
neg_zip: path to zip file of negative videos |
|
""" |
|
start_time = time.time() |
|
if type(input_vids) == str: |
|
input_vids = [input_vids] |
|
for vid in input_vids: |
|
new_input_vid = vid.replace(" ", "_") |
|
os.rename(vid, new_input_vid) |
|
|
|
|
|
if target_prompt not in species_prompt: |
|
species_prompt = f"{species_prompt}, {target_prompt}" |
|
|
|
|
|
target_prompt = target_prompt.split(", ") |
|
|
|
now = datetime.now() |
|
timestamp = now.strftime("%Y-%m-%d_%H-%M") |
|
|
|
save_dir = f"temp_{timestamp}" |
|
|
|
zip_path = owl_batch_video( |
|
input_vids, |
|
target_prompt, |
|
species_prompt, |
|
conf_threshold, |
|
fps_processed=fps_processed, |
|
scaling_factor=1/scaling_factor, |
|
batch_size=BATCH_SIZE, |
|
save_dir=save_dir, |
|
progress=progress) |
|
|
|
|
|
shutil.rmtree(save_dir) |
|
|
|
end_time = time.time() |
|
print(f'Processing time: {end_time - start_time} seconds') |
|
return zip_path |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.HTML( |
|
""" |
|
<h1 align="center" style="font-size:xxx-large">🦍 Primate Detection</h1> |
|
""" |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
input = gr.File(label="Upload Videos", file_types=['.mp4', '.MOV'], file_count="multiple") |
|
target_prompt = gr.Textbox(label="What do you want to detect? (Multiple species should be separated by commas)") |
|
species_prompt = gr.Textbox(label="Which species are in your dataset? (Multiple species should be separated by commas)") |
|
with gr.Accordion("Advanced Options", open=False): |
|
conf_threshold = gr.Slider( |
|
label="Confidence Threshold", |
|
info="Adjust the threshold to change the sensitivity of the model, lower thresholds being more sensitive.", |
|
minimum=0.0, |
|
maximum=1.0, |
|
value=0.3, |
|
step=0.05 |
|
) |
|
fps_processed = gr.Slider( |
|
label="Frame Detection Rate", |
|
info="Adjust the frame detection rate. I.e. a value of 120 will run detection every 120 frames, a value of 1 will run detection on every frame. Note: the lower the number the slower the processing time.", |
|
minimum=1, |
|
maximum=120, |
|
value=10, |
|
step=1) |
|
scaling_factor = gr.Slider( |
|
label="Downsample Factor", |
|
info="Adjust the downsample factor. Note: the higher the number the faster the processing time but lower the accuracy.", |
|
minimum=1, |
|
maximum=10, |
|
value=4, |
|
step=1 |
|
) |
|
with gr.Row(): |
|
clear_btn = gr.ClearButton(components=[input, target_prompt, species_prompt]) |
|
run_btn = gr.Button(value="Run Detection", variant='primary') |
|
with gr.Column(): |
|
download_file = gr.Files(label="CSV, Video Output", interactive=False) |
|
|
|
run_btn.click(fn=run_owl_batch, inputs=[input, target_prompt, species_prompt, conf_threshold, fps_processed, scaling_factor], outputs=[download_file]) |
|
|
|
gr.DuplicateButton() |
|
|
|
gr.Markdown( |
|
""" |
|
## Frequently Asked Questions |
|
|
|
##### How can I run the interface on my own computer? |
|
By clicking on the three dots on the top right corner of the interface, you will be able to clone the repository or run it with a Docker image on your local machine. \ |
|
For local machine setup instructions please check the README file. |
|
##### The video is very slow to process, how can I speed it up? |
|
You can speed up the processing by adjusting the frame detection rate in the advanced options. The lower the number the slower the processing time. Choosing only\ |
|
bounding boxes will make the processing faster. You can also duplicate the space using the Duplicate Button and choose a different GPU which will make the processing faster. |
|
""" |
|
) |
|
|
|
demo.launch(share=True) |