MessDetector/main.py

42 lines
1 KiB
Python
Raw Normal View History

2023-11-07 11:38:19 +00:00
import cv2
import numpy as np
2023-11-08 21:17:08 +00:00
from mobile_sam import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor
import torch
2023-11-07 11:38:19 +00:00
2023-11-08 21:17:08 +00:00
model_type = "vit_t"
sam_checkpoint = "./models/mobile_sam.pt"
backSub = cv2.createBackgroundSubtractorMOG2(varThreshold=16 )
2023-11-07 11:38:19 +00:00
# Load the image
2023-11-08 21:17:08 +00:00
refimage = cv2.imread('clean2.jpeg')
image = cv2.imread('dirty2.jpeg')
res = cv2.absdiff(refimage, image)
res = res.astype(np.uint8)
percentage = (np.count_nonzero(res) * 100)/ res.size
print(percentage)
#cv2.imwrite('/var/www/html/images/blurred.jpg', blurred)
print("Converting to grey")
gray_refimage = cv2.cvtColor(refimage, cv2.COLOR_BGR2GRAY)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred_refimage = cv2.GaussianBlur(gray_refimage, (9, 9), 2, 2)
blurred_image = cv2.GaussianBlur(gray_image, (9, 9), 2, 2)
#cv2.imwrite('/var/www/html/images/gray.jpg', gray)
maska = backSub.apply(blurred_refimage)
maskb = backSub.apply(blurred_image)
cv2.imshow("maskb",maskb)
cv2.waitKey(0)
# closing all open windows
cv2.destroyAllWindows()