-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.py
More file actions
54 lines (46 loc) · 1.93 KB
/
Copy pathBot.py
File metadata and controls
54 lines (46 loc) · 1.93 KB
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
import telebot
import torch
import torchvision.transforms as transforms
from PIL import Image
import pickle
with open('classif_model.pkl', 'rb') as f:
model = pickle.load(f)
# Define a function to preprocess the image and make predictions
def predict_image_class(image):
transform = transforms.Compose([
transforms.Resize((224, 224)), # Resize to match the input size of the model
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
image_tensor = transform(image).unsqueeze(0)
outputs = model(image_tensor)
_, predicted_class = torch.max(outputs, 1)
return predicted_class.item()
# bot = telebot.TeleBot('') Here goes the token
# Start command handler
@bot.message_handler(commands=["start"])
def start(message):
if message.text == "/start":
bot.send_message(message.chat.id,
"Hello Dear User, My name is Area Scout! \n"
"I was programmed to help you understand the area you live in. \n"
"All you have to do is send me the photo of your area and I will tell you if it's an Urban Area or Rural Area")
# Photo handler
@bot.message_handler(content_types=["photo"])
def handle_photo(message):
# Save the received photo
file_id = message.photo[-1].file_id
file_info = bot.get_file(file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open("temp_image.jpg", 'wb') as new_file:
new_file.write(downloaded_file)
# Load and preprocess the image
image_data = Image.open("temp_image.jpg")
predicted_class = predict_image_class(image_data)
# Send the classification result
if predicted_class == 0:
bot.send_message(message.chat.id, "The area is classified as Rural.")
else:
bot.send_message(message.chat.id, "The area is classified as Urban.")
# Start the bot
bot.polling(none_stop=True, interval=0)