Rahatara commited on
Commit
45b94ab
·
verified ·
1 Parent(s): 06ec7fa

Update app2.py

Browse files
Files changed (1) hide show
  1. app2.py +73 -63
app2.py CHANGED
@@ -1,80 +1,90 @@
1
- YOUR_API_KEY= "AIzaSyBjb6LLerzZE6JIIE0YBK6Wn0hqdO9E1Zk"
2
  import cv2
3
  import gradio as gr
4
- import hashlib
5
- import google.generativeai as genai
6
-
7
- genai.configure(api_key="YOUR_API_KEY")
8
-
9
- # Set up the model
10
- generation_config = {
11
- "temperature": 0.4,
12
- "top_p": 1,
13
- "top_k": 32,
14
- "max_output_tokens": 4096,
15
- }
16
-
17
- safety_settings = [
18
- {
19
- "category": "HARM_CATEGORY_HARASSMENT",
20
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
21
- },
22
- {
23
- "category": "HARM_CATEGORY_HATE_SPEECH",
24
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
25
- },
26
- {
27
- "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
28
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
29
- },
30
- {
31
- "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
32
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
33
- },
34
- ]
35
-
36
- model = genai.GenerativeModel(model_name="gemini-1.0-pro-vision-latest",
37
- generation_config=generation_config,
38
- safety_settings=safety_settings)
39
-
40
- uploaded_files = []
41
-
42
- # Function to upload image to Gemini Pro-Vision
43
- def upload_image_for_description(image):
44
- image_encoded = cv2.imencode('.jpg', image)[1]
45
- hash_id = hashlib.sha256(image_encoded).hexdigest()
46
- uploaded_file = genai.upload_file(data=image_encoded.tobytes(), display_name=hash_id)
47
- uploaded_files.append(uploaded_file)
48
- return uploaded_file
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def frame_capture(video_path):
51
  # Function to extract frames
52
  vidObj = cv2.VideoCapture(video_path)
 
 
 
53
  frames = []
54
 
55
- while True:
 
 
 
 
 
56
  success, image = vidObj.read()
57
- if not success:
58
- break
59
- frames.append(image)
60
 
 
 
 
 
 
 
 
 
 
61
  return frames
62
 
63
- def extract_frames_and_describe(video_path):
64
- frames = frame_capture(video_path)
65
- descriptions = []
66
- for frame in frames:
67
- uploaded_file = upload_image_for_description(frame)
68
- try:
69
- description = model.generate_content([uploaded_file, "describe this image"]).prompt[-1].text
70
- descriptions.append(description)
71
- except Exception as e:
72
- print("Error:", e)
73
- return descriptions
74
 
75
  # Define the Gradio interface
76
  video_input = gr.Video(label="Upload Video", autoplay=True)
77
- output_text = gr.outputs.Textbox(label='Description')
78
 
79
  # Create the Gradio app
80
- gr.Interface(fn=extract_frames_and_describe, inputs=video_input, outputs=output_text, title='Video Frame Describer').launch()
 
 
1
  import cv2
2
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+ # get the frames
50
  def frame_capture(video_path):
51
  # Function to extract frames
52
  vidObj = cv2.VideoCapture(video_path)
53
+
54
+ # Used as counter variable
55
+ count = 0
56
  frames = []
57
 
58
+ # checks whether frames were extracted
59
+ success = 1
60
+
61
+ while success:
62
+ # vidObj object calls read
63
+ # function to extract frames
64
  success, image = vidObj.read()
 
 
 
65
 
66
+ # Append the frame to the list
67
+ if success:
68
+ frames.append(image)
69
+ count += 1
70
+
71
+ return frames
72
+
73
+ def extract_frames(video):
74
+ frames = frame_capture(video)
75
  return frames
76
 
77
+
78
+
79
+
80
+
81
+
82
+
83
+
 
 
 
 
84
 
85
  # Define the Gradio interface
86
  video_input = gr.Video(label="Upload Video", autoplay=True)
87
+ output_frames = gr.Gallery(label='Frame')
88
 
89
  # Create the Gradio app
90
+ gr.Interface(fn=extract_frames, inputs=video_input, outputs=output_frames, title='Video Frame Extractor').launch()