Skip to content

Commit

Permalink
Updated Readme File
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh-108272 committed Oct 29, 2024
1 parent fe731cb commit 445f0fc
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions Helmet_Detection/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ This repository contains the implementation of a Helmet Detection Model using Te
- [Training](#training)
- [Evaluation](#evaluation)
- [Usage](#usage)
- [Results](#results)
- [Acknowledgements](#acknowledgements)
- [Result](#Result)

## **Introduction**

Helmet detection is crucial for ensuring safety standards are met in various environments, such as construction sites and roadways. This project leverages a Faster R-CNN approach with a ResNet50 backbone to accurately detect helmets in images.

## **Requirements**

- Python 3.x
- TensorFlow 2.x
- Python
- TensorFlow
- NumPy
- Matplotlib
- lxml
Expand All @@ -46,7 +45,17 @@ The annotations are in XML format. The `parse_annotation` function extracts obje

```python
def parse_annotation(annotation_file):
# Parsing logic
tree = etree.parse(annotation_file)
root = tree.getroot()
objects = []
for obj in root.findall('object'):
obj_struct = {}
obj_struct['name'] = obj.find('name').text
bbox = obj.find('bndbox')
obj_struct['bbox'] = [int(bbox.find('xmin').text), int(bbox.find('ymin').text),
int(bbox.find('xmax').text), int(bbox.find('ymax').text)]
objects.append(obj_struct)
return objects
```

# Loading Data
Expand All @@ -55,7 +64,21 @@ The `load_data` function loads and preprocesses the images and annotations for t

```python
def load_data(annotations_dir, images_dir):
# Loading and preprocessing logic
X, y = [], []
for annotation_file in os.listdir(annotations_dir):
if annotation_file.endswith('.xml'):
annotation_path = os.path.join(annotations_dir, annotation_file)
objects = parse_annotation(annotation_path)
image_path = os.path.join(images_dir, annotation_file.replace('.xml', '.png'))
image = load_img(image_path, target_size=(224, 224))
image = img_to_array(image)
X.append(preprocess_input(image))
labels = [0] * len(classes)
for obj in objects:
label_idx = classes.index(obj['name'])
labels[label_idx] = 1
y.append(labels)
return np.array(X), np.array(y)
```

# Model Architecture
Expand All @@ -64,7 +87,15 @@ The model is based on Faster R-CNN with a ResNet50 backbone, pre-trained on Imag

```python
def create_faster_rcnn():
# Model creation logic
base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(224, 224, 3))
for layer in base_model.layers:
layer.trainable = False
x = base_model.output
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(128, activation='relu')(x)
output = layers.Dense(len(classes), activation='sigmoid')(x)
model = models.Model(inputs=base_model.input, outputs=output)
return model
```

# Training
Expand All @@ -89,14 +120,21 @@ The model can be evaluated on a separate test set. Here, we demonstrate how to l

```python
def preprocess_image(image_path):
# Preprocessing logic
image = load_img(image_path, target_size=(224, 224))
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
image = tf.keras.applications.resnet50.preprocess_input(image)
return image
```


## Interpret Predictions
```python
def interpret_prediction(prediction, threshold=0.6):
# Interpretation logic
if prediction > threshold:
return "With Helmet"
else:
return "Without Helmet"
```

# Usage
Expand All @@ -117,7 +155,7 @@ result = interpret_prediction(prediction[0][0])
display_image_with_prediction(image_path, result)
```

# Result and Accuracy
# Result

The model is working fine with accuracy of 81% and is able to successfully detect the images With Helmets and Without Helmets.

Expand Down

0 comments on commit 445f0fc

Please sign in to comment.