PredictiveManish commited on
Commit
b787ced
·
1 Parent(s): d72eb2b

Files added

Browse files
Files changed (3) hide show
  1. README.md +54 -3
  2. main.py +32 -0
  3. training.py +48 -0
README.md CHANGED
@@ -1,3 +1,54 @@
1
- # Wall Crack Detection Model
2
- This model helps in detecting cracks or deforming walls.
3
- It is created by training CNNs on a large dataset of around 32,000 images (16000-Negative, 32000-Positive) Negative means it had no defect and Positive means it has defects.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Wall Crack Detection
2
+
3
+ This project uses a deep learning model to detect cracks in walls using real-time video feed from a mobile phone camera.
4
+ The project is built using Tensorflow, Keras, and OpenCV.
5
+
6
+
7
+ ## Project Structure
8
+ ```
9
+ |-- crack_detector.h5 # Trained Model
10
+ |--main.py # Real-time detection script
11
+ |--concrete_data/ #Dataset folder (if training from scratch is required)
12
+ ||--train/
13
+ |||-Positive/
14
+ |||-Negative/
15
+ ||--val/
16
+ |||-Positive/
17
+ |||-Negative/
18
+ |--detection_log.csv # Optional, it will help you to store predictions
19
+ |--README.md
20
+ ```
21
+ Requirements
22
+
23
+ ```
24
+ - python 3.10+
25
+ - Tensorflow 2.x
26
+ - OpenCV
27
+ - Numpy
28
+ ```
29
+ Install Dependencies using pip:
30
+ ```
31
+ pip install tensorflow opencv-python numpy
32
+ ```
33
+ For better experience make a virtual environment.
34
+
35
+ 1. conda create --name <environment-name> python=3.10
36
+
37
+ 2. conda activate <environment-name>
38
+ conda deactivate
39
+
40
+ conda activatae <environment-name>
41
+
42
+
43
+ pip install tensorflow opencv-python numpy
44
+
45
+
46
+ conda --version
47
+
48
+ anaconda install
49
+ 1. conda --version
50
+ 2. conda create --name <environment-name> python=3.10
51
+ 3. conda activate <environment-name>
52
+ 4. pip install tensorflow opencv-python numpy
53
+ 5. DroidCam IP in main.py file <url>
54
+ 6. run python main.py
main.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ model = tf.keras.models.load_model("crack_detector.h5")
5
+
6
+ # Use HTTP instead of HTTPS
7
+ url = "<Embedd-your-URL>" #Iinstall droidcam on phone and insert the URL
8
+ cap = cv2.VideoCapture(url)
9
+
10
+ while True:
11
+ ret, frame = cap.read()
12
+ if not ret:
13
+ print("Failed to grab frame. Check IP/Port and make sure phone/laptop are on same WiFi.")
14
+ break
15
+
16
+ img = cv2.resize(frame, (224, 224))
17
+ img = img.astype("float32")/255.0
18
+ img = np.expand_dims(img, axis=0)
19
+
20
+ pred = model.predict(img, verbose=0)[0][0]
21
+ label = "No Crack" if pred<0.9 else "Crack Detected"
22
+
23
+ color = (0,255,0) if label=="No Crack" else (0,0,255)
24
+ cv2.putText(frame, label, (20,40), cv2.FONT_HERSHEY_SIMPLEX,1, color, 2)
25
+
26
+ cv2.imshow("Wall crack detection", frame)
27
+
28
+ if cv2.waitKey(1) & 0xFF == ord('q'):
29
+ break
30
+
31
+ cap.release()
32
+ cv2.destroyAllWindows()
training.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
3
+ from tensorflow.keras.applications import MobileNetV2
4
+ from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
5
+ import os
6
+ from tensorflow.keras.models import Model
7
+ base_path = os.path.expanduser("~/Downloads/chirag-project/concrete_data")
8
+ train_dir = os.path.join(base_path, "train")
9
+ val_dir = os.path.join(base_path, "val")
10
+
11
+ # Data generators
12
+ datagen = ImageDataGenerator(rescale=1./255)
13
+
14
+ train_gen = datagen.flow_from_directory(
15
+ train_dir,
16
+ target_size=(224, 224),
17
+ batch_size=32,
18
+ class_mode="binary"
19
+ )
20
+
21
+ val_gen = datagen.flow_from_directory(
22
+ val_dir,
23
+ target_size=(224, 224),
24
+ batch_size=32,
25
+ class_mode="binary"
26
+ )
27
+
28
+ # Base model
29
+ base_model = MobileNetV2(weights="imagenet", include_top=False, input_shape=(224,224,3))
30
+ x = base_model.output
31
+ x = GlobalAveragePooling2D()(x)
32
+ preds = Dense(1, activation="sigmoid")(x)
33
+
34
+ model = Model(inputs=base_model.input, outputs=preds)
35
+
36
+ # Freeze base layers for transfer learning
37
+ for layer in base_model.layers:
38
+ layer.trainable = False
39
+
40
+ model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
41
+
42
+ # Train
43
+ model.fit(train_gen, validation_data=val_gen, epochs=5)
44
+
45
+ # Save model in repo
46
+ model_save_path = os.path.expanduser("~/Downloads/crack_detector.h5")
47
+ model.save(model_save_path)
48
+ print(f"Model saved as {model_save_path}")