Spaces:
Sleeping
Sleeping
Upload 4 files
Browse filesFiles changed (4)
- DockerfileAdded+17
- app.pyAdded+72
- classification_visual_only.pklAdded+3
- requirements.txtAdded+10
1234567891011121314151617# Use Python 3.11 base image
FROM python:3.11
# Set working directory
WORKDIR /app
# Copy all project files
COPY . .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Expose port 7860 (Hugging Face Spaces requirement)
EXPOSE 7860
# Run the Flask app
CMD ["python", "app.py"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172from flask import Flask, request, jsonify
from flask_cors import CORS
import joblib, os, traceback, requests, numpy as np
from PIL import Image
from moviepy.editor import VideoFileClip
from keras.applications.resnet50 import ResNet50, preprocess_input
from keras.preprocessing import image
app = Flask(__name__)
CORS(app)
# Load models once at startup
classification_model = joblib.load('classification_visual_only.pkl')
resnet_model = ResNet50(weights='imagenet', include_top=False, pooling='avg')
def download_video(video_url, temp_path):
"""Download video from Cloudinary."""
response = requests.get(video_url, stream=True)
with open(temp_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
def extract_video_frames(video_path, interval=5):
"""Extract frames at every `interval` seconds."""
video_clip = VideoFileClip(video_path)
frames = []
for t in range(0, int(video_clip.duration), interval):
frame = video_clip.get_frame(t)
frame_image = Image.fromarray(frame)
frames.append(frame_image)
video_clip.close()
return frames
def extract_visual_features(video_path):
"""Extract average ResNet features from video frames."""
frames = extract_video_frames(video_path)
frame_features = []
for frame in frames:
frame = frame.resize((224, 224))
img_array = image.img_to_array(frame)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
features = resnet_model.predict(img_array)
frame_features.append(features.flatten())
return np.mean(frame_features, axis=0)
@app.route('/classify', methods=['POST'])
def classify_video():
try:
data = request.get_json()
video_url = data.get('video_url')
if not video_url:
return jsonify({'error': 'video_url is required'}), 400
temp_path = 'temp_video.mp4'
download_video(video_url, temp_path)
visual_features = extract_visual_features(temp_path)
prediction = classification_model.predict([visual_features])[0]
os.remove(temp_path)
return jsonify({'prediction': int(prediction)})
except Exception as e:
print(traceback.format_exc())
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)123version https://git-lfs.github.com/spec/v1oid sha256:04d10277bd5ba790f7026e6e93d078a430732efe304c9b0c4cbaab842667d0basize 14688912345678910flask==3.1.2
flask_cors==6.0.1
numpy==2.3.4
joblib==1.5.2
pillow==11.3.0
moviepy==2.0.0.dev2
imageio[ffmpeg]==2.37.0
tensorflow==2.20.0
keras==3.11.3
scikit-learn==1.5.2DockerfileAdded+17
app.pyAdded+72
classification_visual_only.pklAdded+3
requirements.txtAdded+10