0% found this document useful (0 votes)
13 views7 pages

21BEE0103 (Iot 2theory)

Uploaded by

srujan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

21BEE0103 (Iot 2theory)

Uploaded by

srujan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

IOT-Domain Analyst-

Digital Assessment-2

NAME – Lakshit Choudhary

REG. NO – 21BEE0103

SLOT – TCC1

FACULTY – Dr KARTHIKEYAN A
1) Perform object classification using deep learning models and execute in python
by verifying the output using a dataset.

Code :

import torch
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image
model = models.resnet18(pretrained=True)
model.eval()
with open("HNM.txt", "r") as f:
labels = [line.strip() for line in f.readlines()]

# Define image preprocessing pipeline


preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Load and preprocess the image


image = Image.open("C:/Users/LAKSHIT/Desktop/F1.jpg")
input_tensor = preprocess(image)
input_batch = input_tensor.unsqueeze(0)

with torch.no_grad():
output = model(input_batch)

_, predicted_idx = torch.max(output, 1)
predicted_label = labels[predicted_idx.item()]

print("Predicted class label:", predicted_label)


print("Predicted class index:", predicted_idx.item())

Image1 :
Image 2:

Output 1:

Output 2:
CODE :

class NaiveBayesClassifier:
def __init__(self, dataset):
self.dataset = dataset
self.classes = list(dataset.keys())
self.total_instances = sum(sum(self.dataset[class_label].values()) for
class_label in self.classes)

def calculate_prior_probability(self, class_label):


return sum(self.dataset[class_label].values()) / self.total_instances

def calculate_conditional_probability(self, feature, value, class_label):


feature_count = sum(self.dataset[class_label].values())
feature_given_class_count = self.dataset[class_label].get(feature, 0)
return feature_given_class_count / feature_count if feature_count else 0

def predict_class(self, new_instance):


probabilities = {}
for class_label in self.classes:
prior = self.calculate_prior_probability(class_label)
likelihood = 1
for feature, value in new_instance.items():
conditional_prob = self.calculate_conditional_probability(feature,
value, class_label)
likelihood *= conditional_prob
probabilities[class_label] = prior * likelihood

predicted_class = max(probabilities, key=probabilities.get)


return predicted_class

# Given dataset
dataset = {
'Mango': {'Yellow': 350, 'Sweet': 450, 'Long': 0},
'Banana': {'Yellow': 400, 'Sweet': 300, 'Long': 350},
'Others': {'Yellow': 50, 'Sweet': 100, 'Long': 50}
}

classifier = NaiveBayesClassifier(dataset)

new_instance = {'Yellow': 1, 'Sweet': 1, 'Long': 1} # Assuming 1 for present, 0 for


predicted_class = classifier.predict_class(new_instance)
print("Predicted class of the new instance:", predicted_class)
Output:

CODE :

import requests

# ThingSpeak API parameters


WRITE_API_KEY = 'QXHRFKQAB6AELZYQ'
URL = f'https://api.thingspeak.com/update?api_key=QXHRFKQAB6AELZYQ'

# Sample data
field1_data = 27
# Example data for field 1

# Constructing the request URL


data = {'field1': field1_data}

# Sending the POST request


response = requests.post(URL, data=data)

if response.status_code == 200:
print("Data written successfully to ThingSpeak.")
else:
print("Failed to write data to ThingSpeak.")

Output:
Widget :

You might also like