Kazel commited on
Commit
5fd9575
·
verified ·
1 Parent(s): 5faca27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -70
app.py CHANGED
@@ -1,70 +1,57 @@
1
- import gradio as gr
2
- import cv2
3
- import ollama
4
- import threading
5
-
6
- # Initialize the webcam
7
- cap = cv2.VideoCapture(0)
8
-
9
- def query_the_image(query: str, image_list: list[str]):
10
- try:
11
- res = ollama.chat(
12
- model='llava',
13
- options={
14
- 'temperature': 0,
15
- "top_k": 1,
16
- 'top_p': 0.1,
17
- 'mirostat_tau': 1.0,
18
- 'num_ctx': 1024,
19
- 'seed': 42,
20
- 'num_predict': 128
21
- },
22
- messages=[
23
- {
24
- 'role': 'system',
25
- 'content': "You are a home surveillance system. Answer with very short sentences."
26
- },
27
- {
28
- 'role': 'user',
29
- 'content': query,
30
- 'images': image_list,
31
- }
32
- ]
33
- )
34
- return res['message']['content']
35
- except Exception as e:
36
- return f"Error: {e}"
37
-
38
- def get_frame():
39
- ret, frame = cap.read()
40
- if not ret:
41
- return None
42
- _, buffer = cv2.imencode('.jpg', frame)
43
- return buffer.tobytes()
44
-
45
- def process_image(prompt):
46
- frame_data = get_frame()
47
- if frame_data:
48
- return query_the_image(prompt, [frame_data])
49
- return "Error capturing image"
50
-
51
- def video_feed():
52
- while True:
53
- ret, frame = cap.read()
54
- if ret:
55
- yield cv2.imencode('.jpg', frame)[1].tobytes()
56
- else:
57
- break
58
-
59
- gui = gr.Blocks()
60
- with gui:
61
- gr.Markdown("# Live Video AI Assistant")
62
- with gr.Row():
63
- video_component = gr.Video()
64
- threading.Thread(target=video_feed, daemon=True).start()
65
- prompt = gr.Textbox(label="Enter your question")
66
- response = gr.Textbox(label="AI Response")
67
- btn = gr.Button("Ask")
68
- btn.click(process_image, inputs=prompt, outputs=response)
69
-
70
- gui.launch()
 
1
+ import gradio as gr
2
+ import cv2
3
+ import threading
4
+ import torch
5
+ from transformers import BlipProcessor, BlipForConditionalGeneration
6
+ from PIL import Image
7
+
8
+ # Initialize the webcam
9
+ cap = cv2.VideoCapture(0)
10
+
11
+ # Load the Hugging Face model and processor
12
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
13
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-vqa-base").to("cuda" if torch.cuda.is_available() else "cpu")
14
+
15
+ def query_the_image(query: str, image_data: bytes):
16
+ try:
17
+ image = Image.open(io.BytesIO(image_data)).convert("RGB")
18
+ inputs = processor(image, query, return_tensors="pt").to(model.device)
19
+ output = model.generate(**inputs)
20
+ answer = processor.decode(output[0], skip_special_tokens=True)
21
+ return answer
22
+ except Exception as e:
23
+ return f"Error: {e}"
24
+
25
+ def get_frame():
26
+ ret, frame = cap.read()
27
+ if not ret:
28
+ return None
29
+ _, buffer = cv2.imencode('.jpg', frame)
30
+ return buffer.tobytes()
31
+
32
+ def process_image(prompt):
33
+ frame_data = get_frame()
34
+ if frame_data:
35
+ return query_the_image(prompt, frame_data)
36
+ return "Error capturing image"
37
+
38
+ def video_feed():
39
+ while True:
40
+ ret, frame = cap.read()
41
+ if ret:
42
+ yield cv2.imencode('.jpg', frame)[1].tobytes()
43
+ else:
44
+ break
45
+
46
+ gui = gr.Blocks()
47
+ with gui:
48
+ gr.Markdown("# Live Video AI Assistant")
49
+ with gr.Row():
50
+ video_component = gr.Video()
51
+ threading.Thread(target=video_feed, daemon=True).start()
52
+ prompt = gr.Textbox(label="Enter your question")
53
+ response = gr.Textbox(label="AI Response")
54
+ btn = gr.Button("Ask")
55
+ btn.click(process_image, inputs=prompt, outputs=response)
56
+
57
+ gui.launch()