|
| 1 | +# ======================================== |
| 2 | +# Imports |
| 3 | +# ======================================== |
| 4 | +from langchain.chains.llm import LLMChain |
| 5 | +from langchain_core.prompts import PromptTemplate |
| 6 | +from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI |
| 7 | +from langchain.document_loaders import PyPDFLoader |
| 8 | +from langchain_core.messages import HumanMessage, SystemMessage |
| 9 | +from langchain.docstore.document import Document |
| 10 | + |
| 11 | +import streamlit as st |
| 12 | +import io |
| 13 | +import base64 |
| 14 | +import cv2 |
| 15 | +import os |
| 16 | +import ast |
| 17 | +from datetime import timedelta |
| 18 | + |
| 19 | +# ======================================== |
| 20 | +# Helper Functions |
| 21 | +# ======================================== |
| 22 | + |
| 23 | +def encode_image(image_path): |
| 24 | + """Encodes an image to base64 format for LLM input.""" |
| 25 | + with open(image_path, "rb") as image_file: |
| 26 | + return base64.b64encode(image_file.read()).decode("utf-8") |
| 27 | + |
| 28 | + |
| 29 | +def extract_frames(video_path, interval, output_folder="frames"): |
| 30 | + """ |
| 31 | + Extracts frames from a video at a specified interval. |
| 32 | + |
| 33 | + Args: |
| 34 | + video_path (str): Path to the video file. |
| 35 | + interval (int): Frame extraction interval. |
| 36 | + output_folder (str): Directory to store extracted frames. |
| 37 | + |
| 38 | + Returns: |
| 39 | + list: Tuples containing frame file paths and corresponding timecodes. |
| 40 | + """ |
| 41 | + os.makedirs(output_folder, exist_ok=True) |
| 42 | + video_capture = cv2.VideoCapture(video_path) |
| 43 | + frame_count = 0 |
| 44 | + extracted_frames = [] |
| 45 | + frame_rate = int(video_capture.get(cv2.CAP_PROP_FPS)) |
| 46 | + |
| 47 | + while True: |
| 48 | + ret, frame = video_capture.read() |
| 49 | + if not ret: |
| 50 | + break |
| 51 | + |
| 52 | + if frame_count % interval == 0: |
| 53 | + frame_path = os.path.join(output_folder, f"frame_{frame_count}.jpg") |
| 54 | + cv2.imwrite(frame_path, frame) |
| 55 | + timecode = str(timedelta(seconds=frame_count // frame_rate)) |
| 56 | + extracted_frames.append((frame_path, timecode)) |
| 57 | + |
| 58 | + frame_count += 1 |
| 59 | + |
| 60 | + video_capture.release() |
| 61 | + return extracted_frames |
| 62 | + |
| 63 | +# ======================================== |
| 64 | +# Streamlit App UI and Logic |
| 65 | +# ======================================== |
| 66 | + |
| 67 | +def videoAnalyze(): |
| 68 | + # Title of the app |
| 69 | + st.title("Analyze Images and Videos with OCI Generative AI") |
| 70 | + |
| 71 | + # Sidebar inputs |
| 72 | + with st.sidebar: |
| 73 | + st.title("Parameters") |
| 74 | + st.selectbox("Output Language", ["English", "French"]) |
| 75 | + confidenceThreshold = st.slider("Confidence Threshold", 0.0, 1.0) |
| 76 | + st.caption("Adjust the corresponding parameters to control the AI's responses and accuracy") |
| 77 | + interval = st.slider("Select the desired interval: ", 1, 48) |
| 78 | + |
| 79 | + # Optional: Custom styling |
| 80 | + with open('style.css') as f: |
| 81 | + st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True) |
| 82 | + |
| 83 | + # File upload |
| 84 | + uploaded_file = st.file_uploader("Upload an image or video", type=["png", "jpg", "jpeg", "mp4", "avi", "mov"]) |
| 85 | + user_prompt = st.text_input("Enter your prompt for analysis:", value="Is this frame suitable for PG-rated movies?") |
| 86 | + |
| 87 | + if uploaded_file is not None: |
| 88 | + # Save the uploaded file locally |
| 89 | + temp_video_path = "temp_uploaded_video.mp4" |
| 90 | + with open(temp_video_path, "wb") as f: |
| 91 | + f.write(uploaded_file.getbuffer()) |
| 92 | + |
| 93 | + # Check if file is a video |
| 94 | + if uploaded_file.type.startswith("video"): |
| 95 | + # Extract frames at defined interval |
| 96 | + with st.spinner("Extracting frames from the video..."): |
| 97 | + frames_with_timecodes = extract_frames(temp_video_path, interval) |
| 98 | + st.success(f"Extracted {len(frames_with_timecodes)} frames for analysis.") |
| 99 | + |
| 100 | + # Instantiate the OCI Generative AI Vision model |
| 101 | + llm = ChatOCIGenAI( |
| 102 | + model_id="meta.llama-3.2-90b-vision-instruct", |
| 103 | + compartment_id="", # <-- Add your compartment ID here |
| 104 | + model_kwargs={"max_tokens": 2000, "temperature": 0} |
| 105 | + ) |
| 106 | + |
| 107 | + # Loop through each frame for analysis |
| 108 | + violence_detected = False |
| 109 | + for frame_path, timecode in frames_with_timecodes: |
| 110 | + with st.spinner("Analyzing the frame..."): |
| 111 | + try: |
| 112 | + # Prepare the frame and messages |
| 113 | + encoded_frame = encode_image(frame_path) |
| 114 | + human_message = HumanMessage( |
| 115 | + content=[ |
| 116 | + {"type": "text", "text": user_prompt}, |
| 117 | + {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_frame}"}}, |
| 118 | + ] |
| 119 | + ) |
| 120 | + |
| 121 | + system_message = SystemMessage( |
| 122 | + content="You are an expert in assessing the age-appropriateness of visual content. Your task is to analyze the provided image and provide a detailed assessment of its suitability for a PG-rated audience." |
| 123 | + "Respond only in dictionary format. Examples:\n" |
| 124 | + "If the frame contains elements unsuitable for a PG-rating: " |
| 125 | + "{'AgeAppropriate': 'not-appropriate', 'response': 'Brief description of the scene (e.g., shows graphic violence, explicit nudity).', 'ConfidenceLevel': 0.95}\n" |
| 126 | + "If the frame complies with PG-rating guidelines: " |
| 127 | + "{'AgeAppropriate': 'appropriate', 'response': 'Brief description of the scene (e.g., depicts a serene landscape, no concerning elements).', 'ConfidenceLevel': 0.90}\n" |
| 128 | + "Ensure your responses are concise and focused on the image's content. Avoid unnecessary details or conversations unrelated to the task." |
| 129 | + ) |
| 130 | + |
| 131 | + # LLM call |
| 132 | + ai_response = llm.invoke(input=[human_message, system_message]) |
| 133 | + print(ai_response.content) |
| 134 | + response_dict = ast.literal_eval(ai_response.content) |
| 135 | + |
| 136 | + # Parse and validate the response |
| 137 | + violence_status = response_dict.get("AgeAppropriate") |
| 138 | + detailed_response = response_dict.get("response") |
| 139 | + confidence = float(response_dict.get("ConfidenceLevel")) |
| 140 | + |
| 141 | + # Display flagged frames |
| 142 | + if violence_status == "not-appropriate" and confidence >= confidenceThreshold: |
| 143 | + st.write(f"Frame Analysis: {detailed_response}") |
| 144 | + st.write(f"Timecode: {timecode}") |
| 145 | + st.image(frame_path, caption="Analyzing Frame", width=500) |
| 146 | + violence_detected = True |
| 147 | + |
| 148 | + except Exception as e: |
| 149 | + print(f"Error analyzing frame: {str(e)}") |
| 150 | + |
| 151 | + # Final result |
| 152 | + if violence_detected: |
| 153 | + st.warning("This movie is NOT PG Rated!") |
| 154 | + else: |
| 155 | + st.success("This movie is PG Rated!") |
0 commit comments