File size: 1,717 Bytes
298548f
 
 
294d63a
 
 
 
4b8dda6
fd12cf3
298548f
 
294d63a
298548f
294d63a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
298548f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1adecf
1
2
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
50
51
52
import gradio as gr
from PIL import Image
import numpy as np
import requests  # For API calls

# Define your API endpoint and key
API_ENDPOINT = "https://your-api-endpoint.com/predict"  # Replace with your actual API endpoint
API_KEY = "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"  # Replace with your API key

def detect_weapons(image):
    """
    Sends the uploaded image to an API for weapon detection.
    """
    try:
        # Convert the PIL Image to bytes for API upload
        image_bytes = np.array(image).tobytes()
        
        # Prepare headers and payload
        headers = {"Authorization": f"Bearer {API_KEY}"}
        files = {"image": image_bytes}
        
        # Send the image to the API
        response = requests.post(API_ENDPOINT, headers=headers, files=files)
        
        if response.status_code == 200:
            # Parse the API response
            results = response.json()  # Assuming the API returns JSON
            return f"Detected Weapons: {results}"
        else:
            return f"API Error: {response.status_code} - {response.text}"
    except Exception as e:
        return f"Error during detection: {e}"

def process_image(image):
    """
    Function to process the uploaded image for weapon detection.
    """
    results = detect_weapons(image)
    return results

# Create the Gradio Interface
interface = gr.Interface(
    fn=process_image,
    inputs=gr.Image(type="pil", label="Upload an Image"),
    outputs=gr.Textbox(label="Detection Results"),
    title="Weapon Detection App",
    description="Upload an image to detect weapons like guns or bombs."
)

# Launch the Gradio app
interface.launch(server_name="0.0.0.0", server_port=7860)