/ single_img.py
single_img.py
 1  #
 2  # sending sms only once per image in dataset
 3  
 4  
 5  import cv2
 6  import os
 7  import cvzone
 8  from sms import send_msg
 9  thres = 0.55
10  nmsThres = 0.2
11  
12  # Path to your dataset directory containing images
13  dataset_path = r"YOUR_PATH"
14  
15  classNames = []
16  classFile = 'coco.names'
17  with open(classFile, 'rt') as f:
18      classNames = f.read().split('\n')
19  print(classNames)
20  
21  configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
22  weightsPath = "frozen_inference_graph.pb"
23  
24  net = cv2.dnn_DetectionModel(weightsPath, configPath)
25  net.setInputSize(320, 320)
26  net.setInputScale(1.0 / 127.5)
27  net.setInputMean((127.5, 127.5, 127.5))
28  net.setInputSwapRB(True)
29  
30  # Get a list of image files in the dataset directory
31  image_files = [file for file in os.listdir(dataset_path) if file.endswith((".jpg", ".jpeg"))]
32  
33  sms_sent = False  # Flag variable to track SMS sent status
34  
35  for image_file in image_files:
36      img = cv2.imread(os.path.join(dataset_path, image_file))
37      classIds, confs, bbox = net.detect(img, confThreshold=thres, nmsThreshold=nmsThres)
38  
39      # Send SMS only once for each image
40      if not sms_sent:
41          try:
42              send_msg()
43              sms_sent = True  # Set flag to True once SMS is sent
44          except Exception as e:
45              print("Error sending SMS:", e)
46  
47      for classId, conf, box in zip(classIds.flatten(), confs.flatten(), bbox):
48          cvzone.cornerRect(img, box)
49          cv2.putText(img, f'{classNames[classId - 1].upper()} {round(conf * 100, 2)}',
50                      (box[0] + 10, box[1] + 30), cv2.FONT_HERSHEY_COMPLEX_SMALL,
51                      1, (0, 255, 0), 2)
52  
53      cv2.imshow("Image", img)
54      cv2.waitKey(0)
55  
56  cv2.destroyAllWindows()