I have a C# script that uploads a video to a Flask API, the API processes the video using openCV and it creates and sends back a text file. The only issue is, it only seems to work with the .qt filetype, and any other video I send will not work properly (mov, mp4, etc). The API receives a valid video, or so it says, but it is as if the video comes across totally empty with no data in it. The text file it creates is blank because it seemingly is processing nothing. There is nothing in either script that makes the .qt file only work, and I have tried everything including tinkering with the 'MediaTypeHeaderValue.' I am absolutely stumped. Also, I am posting again because my first post on stack overflow wasn't good enough or had all the scripts, so I am just starting over. I'll post the C# script to upload the video, the Python script to process it, and the requirements.txt. C# Code
using System.Net.Http.Headers;
var filePath = "run.mp4";
using var client = new HttpClient();
using (var multipartFormContent = new MultipartFormDataContent())
{
//Add the file
var fileStreamContent = new StreamContent(File.OpenRead(filePath));
fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("video/mp4");
multipartFormContent.Add(fileStreamContent, name: "file", fileName: "run.mp4");
//Send it
var response = await client.PostAsync("http://127.0.0.1:5000/", multipartFormContent);
//Ensure it was successful.
response.EnsureSuccessStatusCode();
//Grab the animation data from the content.
var animation_data = await response.Content.ReadAsStringAsync();
//Save to file.
await File.WriteAllTextAsync("AnimationFile.txt", animation_data);
}
Python Code Below
from flask import Flask, request, redirect, url_for, send_from_directory, request
from werkzeug.utils import secure_filename
import cv2
from cvzone.PoseModule import PoseDetector
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'3gp', 'mov', 'qt', 'avi', 'wmv', 'mp4'}
app = Flask(__name__)
app.config["DEBUG"] = True
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1000 * 1000 # Limit upload size to 16 megabytes per file.
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/uploads/<name>')
def download_file(name):
return send_from_directory(app.config["UPLOAD_FOLDER"], name)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url,400, "File part not found in request.")
file = request.files['file']
if file.filename == '':
return redirect(request.url,400, "Filename is empty.")
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
cap = cv2.VideoCapture(filename)
detector = PoseDetector()
posList = []
while True:
success, img = cap.read()
if img is None:
break
img = detector.findPose(img)
lmList, bboxInfo = detector.findPosition(img)
if bboxInfo:
lmString = ''
for lm in lmList:
lmString += f'{lm[1]},{img.shape[0] - lm[2]},{lm[3]},'
posList.append(lmString)
# file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) Don't need to save the file
with open("uploads/AnimationFile.txt", 'w') as f:
f.writelines(["%s\n" % item for item in posList])
return redirect(url_for('download_file', name="AnimationFile.txt"))
return '''<h1>Test Api!</h1>'''
app.run()
Requirements.txt
absl-py==1.2.0
attrs==22.1.0
click==8.1.3
cvzone==1.5.6
cycler==0.11.0
Flask==2.2.2
fonttools==4.37.1
itsdangerous==2.1.2
Jinja2==3.1.2
kiwisolver==1.4.4
MarkupSafe==2.1.1
matplotlib==3.5.3
mediapipe==0.8.10.1
numpy==1.23.2
opencv-contrib-python==4.6.0.66
opencv-python==4.6.0.66
packaging==21.3
Pillow==9.2.0
protobuf==3.20.1
pyparsing==3.0.9
python-dateutil==2.8.2
six==1.16.0
Werkzeug==2.2.2