flask is kind of web application tool, stronger than gradio, but need to write html and javascript -- li

This commit is contained in:
memoryunreal
2023-04-16 05:37:05 +00:00
parent 11c1ef7afb
commit 42f92854f5
4 changed files with 172 additions and 0 deletions

23
app_test.py Normal file
View File

@@ -0,0 +1,23 @@
import gradio as gr
def update_iframe(slider_value):
return f'''
<script>
window.addEventListener('message', function(event) {{
if (event.data.sliderValue !== undefined) {{
var iframe = document.getElementById("text_iframe");
iframe.src = "http://localhost:5001/get_text?slider_value=" + event.data.sliderValue;
}}
}}, false);
</script>
<iframe id="text_iframe" src="http://localhost:5001/get_text?slider_value={slider_value}" style="width: 100%; height: 100%; border: none;"></iframe>
'''
iface = gr.Interface(
fn=update_iframe,
inputs=gr.inputs.Slider(minimum=0, maximum=100, step=1, default=50),
outputs=gr.outputs.HTML(),
allow_flagging=False,
)
iface.launch(server_name='0.0.0.0', server_port=12212)

27
template.html Normal file
View File

@@ -0,0 +1,27 @@
<!-- template.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gradio Video Pause Time</title>
</head>
<body>
<video id="video" controls>
<source src="{{VIDEO_URL}}" type="video/mp4">
Your browser does not support the video tag.
</video>
<script>
const video = document.getElementById("video");
let pauseTime = null;
video.addEventListener("pause", () => {
pauseTime = video.currentTime;
});
function getPauseTime() {
return pauseTime;
}
</script>
</body>
</html>

50
templates/index.html Normal file
View File

@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video Object Segmentation</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Video Object Segmentation</h1>
<input type="file" id="video-input" accept="video/*">
<button id="upload-video">Upload Video</button>
<br>
<button id="template-select">Template Select</button>
<button id="sam-refine">SAM Refine</button>
<br>
<button id="track-video">Track Video</button>
<button id="track-image">Track Image</button>
<br>
<a href="/download_video" id="download-video" download>Download Video</a>
<script>
// JavaScript code for handling interactions with the server
$("#upload-video").click(function() {
var videoInput = document.getElementById("video-input");
var formData = new FormData();
formData.append("video", videoInput.files[0]);
$.ajax({
url: "/upload_video",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(response) {
console.log(response);
// Process the response and update the UI accordingly
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
</script>
</body>
</html>

72
text_server.py Normal file
View File

@@ -0,0 +1,72 @@
import os
import sys
import cv2
import time
import json
import queue
import numpy as np
import requests
import concurrent.futures
from PIL import Image
from flask import Flask, render_template, request, jsonify, send_file
import torchvision
import torch
from demo import automask_image_app, automask_video_app, sahi_autoseg_app
sys.path.append(sys.path[0] + "/tracker")
sys.path.append(sys.path[0] + "/tracker/model")
from track_anything import TrackingAnything
from track_anything import parse_augment
# ... (all the functions defined in the original code except the Gradio part)
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = './uploaded_videos'
app.config['ALLOWED_EXTENSIONS'] = {'mp4', 'avi', 'mov', 'mkv'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route("/")
def index():
return render_template("index.html")
@app.route("/upload_video", methods=["POST"])
def upload_video():
# ... (handle video upload and processing)
return jsonify(status="success", data=video_data)
@app.route("/template_select", methods=["POST"])
def template_select():
# ... (handle template selection and processing)
return jsonify(status="success", data=template_data)
@app.route("/sam_refine", methods=["POST"])
def sam_refine_request():
# ... (handle sam refine and processing)
return jsonify(status="success", data=sam_data)
@app.route("/track_video", methods=["POST"])
def track_video():
# ... (handle video tracking and processing)
return jsonify(status="success", data=tracking_data)
@app.route("/track_image", methods=["POST"])
def track_image():
# ... (handle image tracking and processing)
return jsonify(status="success", data=tracking_data)
@app.route("/download_video", methods=["GET"])
def download_video():
try:
return send_file("output.mp4", attachment_filename="output.mp4")
except Exception as e:
return str(e)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=args.port)
if __name__ == '__main__':
app.run(host="0.0.0.0",port=12212, debug=True)