File size: 4,407 Bytes
a176aa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""

Created on Tue Dec 10 17:15:04 2024



@author: jishu

"""

from Backend.Fruit_Freshness.Apples import shape as sp
from Backend.Fruit_Freshness.Apples import mask as mk
from Backend.Fruit_Freshness.Apples import hue as hu
from Database.mongodb import DatabaseManager

import cv2
import numpy as np

    
def run(image, stale_flag=False, fresh_flag=False, score=10):
    
    # Case 1: Fresh Apple
    case_1 = """

    Fresh Apple

    Shelf-Life: 5-7 days (at room temperature)

    Characteristics: Firm texture, bright and vibrant color, no visible bruises or spots, a sweet and crisp taste.

    Eatable or not: Definitely eatable.

    """
    
    # Case 2: Moderately Stale Apple
    case_2 = """

    Moderately Stale Apple

    Shelf-Life: 8-14 days (room temperature) or 4 weeks (refrigerated)

    Characteristics : Slightly softer texture, slight discoloration or dull appearance, taste may be slightly sour but still acceptable.

    Eatable or not: Eatable but should be consumed soon.

    """
    
    # Case 3: Rotten Apple
    case_3 = """

    Rotten Apple

    Shelf-Life: Exceeds 14 days (room temperature) or 4 weeks (refrigerated)

    Characteristics: Mushy texture, dark or blackened spots, foul smell, and signs of mold or fermentation.

    Eatable or not: Not eatable.

    """
    
    # Case 4: Rotten Apple
    case_4 = """

    Rotten Apple

    Shelf-Life: Exceeds 14 days (room temperature) or 4 weeks (refrigerated)

    Characteristics: Mushy texture even though no blackened spots or fermentation, foul smell.

    Eatable or not: Not eatable.

    """
    # First Check for Colour COncentration
    orange_percentage, yellow_percentage = hu.find_orange_yellow_frequency(image)
    
    if not fresh_flag:
        if yellow_percentage > 50:
            score = 10
            fresh_flag = True
            
        
    if not fresh_flag:
        if orange_percentage > 10:
            score = 5
            
    # Then Check if there are wrinkles on the fruit
    block_size = (30, 30)  # Size of each block
    densities = sp.calculate_edge_density(image, block_size)
            
    if not fresh_flag:
        if densities > 1.2:
            score = score - 2

    # Assume you already have a mask for the fruit (from previous steps)
    hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    lower_bound = np.array([0, 100, 100])  # Example: Orange-ish fruit
    upper_bound = np.array([10, 150, 255])
    binary_mask = cv2.inRange(hsv_image, lower_bound, upper_bound)
    
    kernel = np.ones((5, 5), np.uint8)
    refined_mask = cv2.morphologyEx(binary_mask, cv2.MORPH_CLOSE, kernel)
    refined_mask = cv2.morphologyEx(refined_mask, cv2.MORPH_OPEN, kernel)

    # Detect low pixel values in the fruit area using the mask
    high_pixel_count = mk.detect_low_pixel_values(image, refined_mask)
    
    if not fresh_flag:
        if high_pixel_count > 8:
            stale_flag = True
    
    
    if stale_flag:
        score = 0
        
    answer = ""
    db_manager = DatabaseManager()
    
    if score <=3 and score >= 0:
        if stale_flag:
            answer = case_3
            db_manager.add_freshness_record("Apple", "Exceeds 14 days (room temperature) or 4 weeks (refrigerated)", "Mushy texture, dark or blackened spots, foul smell, and signs of mold or fermentation.", "Not eatable.")
        else:
            answer = case_4
            db_manager.add_freshness_record("Apple", "Exceeds 14 days (room temperature) or 4 weeks (refrigerated)", "Mushy texture even though no blackened spots or fermentation, foul smell.", "Not eatable.")
            
    elif score >= 4 and score <= 7:
        answer = case_2
        db_manager.add_freshness_record("Apple", "8-14 days (room temperature) or 4 weeks (refrigerated)", "Slightly softer texture, slight discoloration or dull appearance, taste may be slightly sour but still acceptable.", "Eatable but should be consumed soon.")
        
    else:
        answer = case_1
        db_manager.add_freshness_record("Apple", "5-7 days (at room temperature)", "Firm texture, bright and vibrant color, no visible bruises or spots, a sweet and crisp taste.", "Definitely eatable.")
        
    return answer