Pranavpai0309 commited on
Commit
3812a7b
·
verified ·
1 Parent(s): 2a263c9

Added files to spaces

Browse files
Files changed (4) hide show
  1. Dockerfile +14 -0
  2. ModelCode.py +51 -0
  3. app.py +45 -0
  4. requirements.txt +12 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ COPY --chown=user . /app
13
+
14
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
ModelCode.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ from transformers import CLIPProcessor, CLIPModel
4
+ from PIL import Image
5
+ import cv2
6
+ from collections import Counter
7
+
8
+ clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
9
+ clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
10
+
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ clip_model = clip_model.to(device)
13
+
14
+ def extract_frames(video_path, frame_rate=5):
15
+ cap = cv2.VideoCapture(video_path)
16
+ fps = cap.get(cv2.CAP_PROP_FPS)
17
+ frames = []
18
+ count = 0
19
+
20
+ while cap.isOpened():
21
+ ret, frame = cap.read()
22
+ if not ret:
23
+ break
24
+ if int(count % (fps * frame_rate)) == 0:
25
+ img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
26
+ frames.append(img)
27
+ count += 1
28
+
29
+ cap.release()
30
+ return frames
31
+
32
+ def classify_frame_with_clip(image):
33
+ texts = ["Ayurveda", "Non-Ayurveda"]
34
+ inputs = clip_processor(text=texts, images=image, return_tensors="pt", padding=True).to(device)
35
+ outputs = clip_model(**inputs)
36
+ logits_per_image = outputs.logits_per_image
37
+ probs = logits_per_image.softmax(dim=1)
38
+ pred = torch.argmax(probs, dim=1).item()
39
+ return texts[pred]
40
+
41
+ def classify_video(video_path):
42
+ frames = extract_frames(video_path, frame_rate=5)
43
+
44
+ clip_preds = []
45
+
46
+ for frame in frames:
47
+ clip_result = classify_frame_with_clip(frame)
48
+ clip_preds.append(clip_result)
49
+
50
+ final_pred = Counter(clip_preds).most_common(1)[0][0]
51
+ return {"Type": final_pred}
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ import os
4
+ import cv2
5
+ from PIL import Image
6
+ from ModelCode import classify_video
7
+
8
+ app = FastAPI()
9
+
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"],
13
+ allow_credentials=True,
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+
19
+ @app.get("/")
20
+ async def root():
21
+ return {"message": "Welcome to Video Classification API!"}
22
+
23
+
24
+ @app.post("/Video_Processing")
25
+ async def process_video(file: UploadFile = File(...)):
26
+ try:
27
+ file_path = f"./{file.filename}"
28
+ if not file.filename.endswith(('.mp4', '.avi', '.mov')):
29
+ return {"error": "File is not a supported video format"}
30
+
31
+ with open(file_path, "wb") as video_file:
32
+ video_file.write(await file.read())
33
+
34
+ result = classify_video(file_path)
35
+
36
+ return {
37
+ "Type": result["Type"],
38
+ }
39
+
40
+ except Exception as e:
41
+ return {"error": str(e)}
42
+
43
+ finally:
44
+ if os.path.exists(file_path):
45
+ os.remove(file_path)
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ torch
4
+ transformers
5
+ opencv-python
6
+ pillow
7
+ pytesseract
8
+ python-multipart
9
+ scikit-learn
10
+ fastapi[all]
11
+ tqdm
12
+ numpy