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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
from pyzbar.pyzbar import decode
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
import os
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from tensorflow.keras.models import load_model
|
| 11 |
+
from tensorflow.keras.preprocessing import image
|
| 12 |
+
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
# Load the pre-trained model
|
| 16 |
+
model = load_model('model.h5')
|
| 17 |
+
|
| 18 |
+
# Class index to category name mapping
|
| 19 |
+
class_names = {
|
| 20 |
+
0: 'Air_Conditioners',
|
| 21 |
+
1: 'Coffee_Espresso_Machines',
|
| 22 |
+
2: 'Computer_Monitors',
|
| 23 |
+
3: 'Televisions',
|
| 24 |
+
4: 'Ironing',
|
| 25 |
+
5: 'Televisions',
|
| 26 |
+
6: 'Vacuums'
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# Category to tips mapping
|
| 30 |
+
tips = {
|
| 31 |
+
"Air_Conditioners": [
|
| 32 |
+
"Set the thermostat to a higher temperature when you're not at home.",
|
| 33 |
+
"Use ceiling fans to circulate air; this allows you to raise the thermostat setting by about 4°F with no reduction in comfort.",
|
| 34 |
+
"Regularly clean or replace air filters to maintain efficiency and performance of the air conditioning system."
|
| 35 |
+
],
|
| 36 |
+
"Coffee_Espresso_Machines": [
|
| 37 |
+
"Turn off the machine when not in use, especially those that heat water continuously.",
|
| 38 |
+
"Use a manual or programmable coffee maker that can be scheduled to turn on only when you need coffee and turn off immediately after.",
|
| 39 |
+
"Preheat your cup with hot water from the tap to avoid using extra energy to heat the cup with the machine."
|
| 40 |
+
],
|
| 41 |
+
"Computer_Monitors": [
|
| 42 |
+
"Turn off the monitor when not in use to save energy.",
|
| 43 |
+
"Adjust the brightness settings to lower levels which can significantly reduce power consumption.",
|
| 44 |
+
"Use energy-saving modes like sleep or hibernate which minimize energy use when the monitor is not actively being used."
|
| 45 |
+
],
|
| 46 |
+
"Ironing": [
|
| 47 |
+
"Use the iron’s steam function to reduce ironing time by efficiently removing wrinkles.",
|
| 48 |
+
"Iron large batches of clothing at once to avoid reheating the iron multiple times.",
|
| 49 |
+
"Turn off the iron a few minutes before finishing, using the residual heat to complete your ironing."
|
| 50 |
+
],
|
| 51 |
+
"Televisions": [
|
| 52 |
+
"Turn off the TV when not in use.",
|
| 53 |
+
"Adjust the brightness to a lower, comfortable level.",
|
| 54 |
+
"Use a smart power strip to prevent phantom energy drain."
|
| 55 |
+
],
|
| 56 |
+
"Vacuums": [
|
| 57 |
+
"Keep the vacuum filters and brushes clean to maintain suction and efficiency.",
|
| 58 |
+
"Use the appropriate settings and attachments for different types of flooring to optimize suction and reduce time spent vacuuming.",
|
| 59 |
+
"Vacuum multiple rooms in a sequence to avoid frequent starts and stops that increase energy usage."
|
| 60 |
+
]
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def read_barcode(image):
|
| 65 |
+
pil_image = Image.fromarray(image)
|
| 66 |
+
decoded_objects = decode(pil_image)
|
| 67 |
+
barcodes = [obj.data.decode("utf-8") for obj in decoded_objects]
|
| 68 |
+
return barcodes[0] if barcodes else None
|
| 69 |
+
|
| 70 |
+
def get_product_info(barcode):
|
| 71 |
+
primary_api_key = "88ABB59A67B72D87CC80C7FAB80E6632"
|
| 72 |
+
fallback_api_key = "0aj97xf02hji3h4wep2tlkldc98zf2"
|
| 73 |
+
primary_url = f"https://api.upcdatabase.org/product/{barcode}?apikey={primary_api_key}"
|
| 74 |
+
fallback_url = f"https://api.barcodelookup.com/v3/products?barcode={barcode}&formatted=y&key={fallback_api_key}"
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
response = requests.get(primary_url)
|
| 78 |
+
if response.status_code == 200 and response.json().get('valid', False):
|
| 79 |
+
return (response.json(),1)
|
| 80 |
+
else:
|
| 81 |
+
response = requests.get(fallback_url)
|
| 82 |
+
return (response.json(),2) if response.status_code == 200 else None
|
| 83 |
+
except requests.RequestException as e:
|
| 84 |
+
return (False,False)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def preprocess_image(img):
|
| 89 |
+
img = image.img_to_array(img)
|
| 90 |
+
img = image.smart_resize(img, (150, 150)) # Resizing the image to the required dimensions
|
| 91 |
+
img = np.expand_dims(img, axis=0)
|
| 92 |
+
return preprocess_input(img)
|
| 93 |
+
|
| 94 |
+
def predict_image(img):
|
| 95 |
+
processed_image = preprocess_image(img)
|
| 96 |
+
prediction = model.predict(processed_image)
|
| 97 |
+
predicted_class_index = np.argmax(prediction, axis=1)[0]
|
| 98 |
+
predicted_class_name = class_names[predicted_class_index]
|
| 99 |
+
tips_text = "\n".join(tips[predicted_class_name])
|
| 100 |
+
return predicted_class_index,predicted_class_name, tips_text
|
| 101 |
+
|
| 102 |
+
def process_image(image):
|
| 103 |
+
barcode = read_barcode(image)
|
| 104 |
+
if barcode:
|
| 105 |
+
product_info,API = get_product_info(barcode)
|
| 106 |
+
print(barcode)
|
| 107 |
+
if product_info:
|
| 108 |
+
if barcode=="0887276371375":
|
| 109 |
+
energy_info = {
|
| 110 |
+
"Energy_Characteristics": [
|
| 111 |
+
"Product meets ENERGY STAR guidelines for energy efficiency.",
|
| 112 |
+
"Power management settings enabled by default, with timing settings from 1 minute to 5 hours.",
|
| 113 |
+
"Device can wake up with a button press on the chassis from sleep mode."
|
| 114 |
+
],
|
| 115 |
+
"Tips_to_Save_Energy": [
|
| 116 |
+
"Adjust the LCD brightness to reduce eye strain and save power.",
|
| 117 |
+
"Use power management settings to enable power-saving mode when not in use or after set inactivity period.",
|
| 118 |
+
"Fully charge the battery before using the computer for the first time to ensure optimal battery performance and longevity."
|
| 119 |
+
],
|
| 120 |
+
"Energy_Consumption_Estimation_in_Different_Modes": [
|
| 121 |
+
"Power Saving Mode: Consumes minimal energy when LCD is closed or there is no input for a predetermined period.",
|
| 122 |
+
"Battery Use: Higher energy consumption during battery charging, especially if the computer is used during charging.",
|
| 123 |
+
"On/Off Modes: Energy consumption varies significantly between active use, sleep mode, and fully turned off, with sleep mode designed to save energy."
|
| 124 |
+
]
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
elif barcode=="0719192596764":
|
| 128 |
+
energy_info = {
|
| 129 |
+
"Energy_Characteristics": [
|
| 130 |
+
"Power Requirement: AC 100 - 240 V ~ 50 / 60 Hz.",
|
| 131 |
+
"Current Value/Power Consumption varies by model (e.g., 0.9 A / 90 W for 40LF6300, 1.6 A / 160 W for 65LF6300).",
|
| 132 |
+
"Product qualifies for ENERGY STAR in the factory default (Home Use) setting; altering factory default settings may increase power consumption beyond ENERGY STAR qualification."
|
| 133 |
+
],
|
| 134 |
+
"Tips_to_Save_Energy": [
|
| 135 |
+
"Utilize the TV's energy-saving settings to reduce power consumption.",
|
| 136 |
+
"Adjust the screen brightness to lower settings to save power.",
|
| 137 |
+
"Ensure the TV is fully turned off and not in standby mode when not in use to minimize energy consumption."
|
| 138 |
+
],
|
| 139 |
+
"Energy_Consumption_Estimation_in_Different_Modes": [
|
| 140 |
+
"Standby Mode: Minimal energy usage, but power is still consumed unless the device is fully unplugged.",
|
| 141 |
+
"Active Mode (Energy Saving Mode On): Reduces power usage; specific power consumption values are lower than in normal operating mode.",
|
| 142 |
+
"Active Mode (Normal): Consumption corresponds to the listed current values/power consumption figures depending on model and settings."
|
| 143 |
+
]
|
| 144 |
+
}
|
| 145 |
+
data=product_info
|
| 146 |
+
|
| 147 |
+
if API==1:
|
| 148 |
+
data=data['title']
|
| 149 |
+
elif API==2:
|
| 150 |
+
data=data["products"][0]["title"]
|
| 151 |
+
|
| 152 |
+
return barcode, data, json.dumps(energy_info, indent=4)
|
| 153 |
+
else:
|
| 154 |
+
|
| 155 |
+
return barcode, "Product information not found", "{}"
|
| 156 |
+
else:
|
| 157 |
+
predicted_class_index,predicted_class_name,tips_text=predict_image(image)
|
| 158 |
+
print(predicted_class_index)
|
| 159 |
+
print(predicted_class_name)
|
| 160 |
+
print(tips_text)
|
| 161 |
+
data = {"tips": tips_text}
|
| 162 |
+
json_data = json.dumps(data, indent=4)
|
| 163 |
+
|
| 164 |
+
return predicted_class_index, predicted_class_name, json_data
|
| 165 |
+
|
| 166 |
+
interface = gr.Interface(
|
| 167 |
+
fn=process_image,
|
| 168 |
+
inputs=[gr.Image(type="numpy")],
|
| 169 |
+
outputs=[gr.Textbox(label="Barcode/Index"), gr.Textbox(label="Product Information"), gr.JSON(label="Energy Info")]
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
interface.launch(share=True)
|