File size: 5,984 Bytes
fde8a6a
 
 
1a3879b
 
fde8a6a
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e6be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fde8a6a
d8e6be1
fde8a6a
d8e6be1
fde8a6a
 
8071fb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e6be1
 
fde8a6a
 
8071fb2
fde8a6a
 
 
 
 
 
 
d8e6be1
fde8a6a
 
d8e6be1
fde8a6a
 
8071fb2
 
 
 
 
fde8a6a
8071fb2
fde8a6a
 
 
 
 
 
d8e6be1
fde8a6a
 
 
 
 
 
d8e6be1
 
8071fb2
 
 
 
d8e6be1
 
 
 
 
8071fb2
 
 
 
 
fde8a6a
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
import gradio as gr
import google.generativeai as genai
import matplotlib.pyplot as plt
import numpy as np

# Configure Gemini API
GEMINI_API_KEY =  os.environ["GEMINI_API_KEY"]
genai.configure(api_key=GEMINI_API_KEY)

# Create the model with the same configuration as the sample
generation_config = {
    "temperature": 0.2,
    "top_p": 0.95,
    "top_k": 40,
    "max_output_tokens": 8192,
    "response_mime_type": "text/plain",
}

SYSTEM_INSTRUCTION = """You are an expert fish quality analyser. You use following criteria to identify the quality of the fish and score them out of 10 points.

Primary Key Indicators for Fish Quality Assessment
1. Eyes
Freshness Indicator: The eyes should be clear, bright, and bulging with a metallic sheen.
Deterioration Signs: If the eyes appear cloudy, sunken, or have a greyish color, it indicates that the fish is not fresh and may be decomposing.
2. Gills
Freshness Indicator: Gills should be bright red or pink, indicating fresh blood and optimal freshness.
Deterioration Signs: Gills turning brown, grey, or green suggest that the fish is past its prime and potentially spoiled.
3. Skin Appearance
Freshness Indicator: The skin should have a shiny, mother-of-pearl sheen and be free of blemishes or discoloration.
Deterioration Signs: Dullness or yellowing of the skin indicates degradation and loss of freshness.
4. Odor
Freshness Indicator: Fresh fish typically has a neutral smell with slight hints of seaweed.
Deterioration Signs: A sour, off, or overly strong odor suggests spoilage.
5. Texture
Freshness Indicator: The flesh should be firm to the touch and spring back when pressed.
Deterioration Signs: Softness or mushiness indicates that the fish is no longer fresh.
6. Color of Flesh
Freshness Indicator: The flesh should have a vibrant color appropriate for the species (e.g., pink for salmon).
Deterioration Signs: Any discoloration or dullness in the flesh can indicate spoilage.
7. Overall Appearance
Freshness Indicator: The overall appearance should be appealing without any signs of drying out or excessive slime.
Deterioration Signs: Excessive slime or drying out can indicate that the fish is not fresh."""

model = genai.GenerativeModel(
    model_name="gemini-2.0-flash-exp",
    generation_config=generation_config,
    system_instruction=SYSTEM_INSTRUCTION,
)

def create_quality_plot(quality, score):
    """Create a visualization of the fish quality score"""
    plt.figure(figsize=(10, 4))
    
    # Create bar chart
    score = float(score)  # Convert score to float
    bar_color = 'red' if score <= 4 else ('yellow' if score <= 7 else 'green')
    
    plt.bar(['Quality Score'], [score], color=bar_color)
    plt.axhline(y=10, color='gray', linestyle='--', alpha=0.5)
    
    # Customize plot
    plt.ylim(0, 11)
    plt.title(f'Fish Quality Assessment: {quality}', pad=20)
    plt.ylabel('Score (0-10)')
    
    # Add quality zones
    plt.axhspan(0, 4, alpha=0.1, color='red', label='Bad (0-4)')
    plt.axhspan(4, 7, alpha=0.1, color='yellow', label='Average (4-7)')
    plt.axhspan(7, 10, alpha=0.1, color='green', label='Good (7-10)')
    
    # Add legend
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
    
    # Save plot to a temporary file
    plt.tight_layout()
    temp_plot = "temp_plot.png"
    plt.savefig(temp_plot, bbox_inches='tight', dpi=300)
    plt.close()
    
    return temp_plot

def parse_response(response_text):
    """Parse the model's response to extract quality and score"""
    try:
        lines = response_text.strip().split('\n')
        quality = lines[0].split(': ')[1].strip()
        score = lines[1].split(': ')[1].strip().strip('()')
        return quality, score
    except:
        return "Bad", "0"

def analyze_fish_quality(image):
    """Analyze the fish quality using Gemini model"""
    try:
        if image is None:
            return "Please upload an image to analyze.", None
            
        # Start a new chat session
        chat = model.start_chat()
        
        # Upload and analyze the image
        image_file = genai.upload_file(image)
        
        # Send the image with the instruction
        response = chat.send_message([
            image_file,
            "Now provided the image of a fish, give me two output. Dont generate any more extra text other then the following.\n1. Fish Quality: Good/Average/Bad\n2. Quality Score (0-10): "
        ])
        
        # Parse response and create visualization
        quality, score = parse_response(response.text)
        plot_path = create_quality_plot(quality, score)
        
        return response.text, plot_path
    except Exception as e:
        return f"Error analyzing image: {str(e)}\nPlease make sure you've uploaded a valid image file.", None

# Get example images from the examples directory
examples_dir = os.path.join(os.path.dirname(__file__), "examples")
example_images = []
if os.path.exists(examples_dir):
    example_images = [
        os.path.join(examples_dir, f) 
        for f in os.listdir(examples_dir) 
        if f.lower().endswith(('.png', '.jpg', '.jpeg'))
    ]

# Create Gradio interface
iface = gr.Interface(
    fn=analyze_fish_quality,
    inputs=gr.Image(type="filepath", label="Upload Fish Image"),
    outputs=[
        gr.Textbox(label="Quality Analysis Result", lines=4),
        gr.Image(label="Quality Score Visualization")
    ],
    title="Fish Quality Analyzer",
    description="""Upload an image of a fish to analyze its quality. The system will evaluate:
    - Overall quality (Good/Average/Bad)
    - Quality score (0-10)
    
    The analysis is based on various factors including eyes, gills, skin appearance, color, and overall condition.
    The visualization shows the quality score with color-coded zones:
    - Green: Good (7-10)
    - Yellow: Average (4-7)
    - Red: Bad (0-4)""",
    examples=example_images,
    cache_examples=True
)

# Launch the app
if __name__ == "__main__":
    iface.launch()