Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#20413 YOLOE: Fix visual prompt training

Merged
Ghost merged 1 commits into Ultralytics:main from ultralytics:yoloe-vp-fix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  1. # Ultralytics ๐Ÿš€ AGPL-3.0 License - https://ultralytics.com/license
  2. from ultralytics.solutions.solutions import BaseSolution, SolutionAnnotator, SolutionResults
  3. from ultralytics.utils import LOGGER
  4. from ultralytics.utils.plotting import colors
  5. class SecurityAlarm(BaseSolution):
  6. """
  7. A class to manage security alarm functionalities for real-time monitoring.
  8. This class extends the BaseSolution class and provides features to monitor objects in a frame, send email
  9. notifications when specific thresholds are exceeded for total detections, and annotate the output frame for
  10. visualization.
  11. Attributes:
  12. email_sent (bool): Flag to track if an email has already been sent for the current event.
  13. records (int): Threshold for the number of detected objects to trigger an alert.
  14. server (smtplib.SMTP): SMTP server connection for sending email alerts.
  15. to_email (str): Recipient's email address for alerts.
  16. from_email (str): Sender's email address for alerts.
  17. Methods:
  18. authenticate: Set up email server authentication for sending alerts.
  19. send_email: Send an email notification with details and an image attachment.
  20. process: Monitor the frame, process detections, and trigger alerts if thresholds are crossed.
  21. Examples:
  22. >>> security = SecurityAlarm()
  23. >>> security.authenticate("abc@gmail.com", "1111222233334444", "xyz@gmail.com")
  24. >>> frame = cv2.imread("frame.jpg")
  25. >>> results = security.process(frame)
  26. """
  27. def __init__(self, **kwargs):
  28. """
  29. Initialize the SecurityAlarm class with parameters for real-time object monitoring.
  30. Args:
  31. **kwargs (Any): Additional keyword arguments passed to the parent class.
  32. """
  33. super().__init__(**kwargs)
  34. self.email_sent = False
  35. self.records = self.CFG["records"]
  36. self.server = None
  37. self.to_email = ""
  38. self.from_email = ""
  39. def authenticate(self, from_email, password, to_email):
  40. """
  41. Authenticate the email server for sending alert notifications.
  42. Args:
  43. from_email (str): Sender's email address.
  44. password (str): Password for the sender's email account.
  45. to_email (str): Recipient's email address.
  46. This method initializes a secure connection with the SMTP server and logs in using the provided credentials.
  47. Examples:
  48. >>> alarm = SecurityAlarm()
  49. >>> alarm.authenticate("sender@example.com", "password123", "recipient@example.com")
  50. """
  51. import smtplib
  52. self.server = smtplib.SMTP("smtp.gmail.com: 587")
  53. self.server.starttls()
  54. self.server.login(from_email, password)
  55. self.to_email = to_email
  56. self.from_email = from_email
  57. def send_email(self, im0, records=5):
  58. """
  59. Send an email notification with an image attachment indicating the number of objects detected.
  60. Args:
  61. im0 (numpy.ndarray): The input image or frame to be attached to the email.
  62. records (int): The number of detected objects to be included in the email message.
  63. This method encodes the input image, composes the email message with details about the detection, and sends it
  64. to the specified recipient.
  65. Examples:
  66. >>> alarm = SecurityAlarm()
  67. >>> frame = cv2.imread("path/to/image.jpg")
  68. >>> alarm.send_email(frame, records=10)
  69. """
  70. from email.mime.image import MIMEImage
  71. from email.mime.multipart import MIMEMultipart
  72. from email.mime.text import MIMEText
  73. import cv2
  74. img_bytes = cv2.imencode(".jpg", im0)[1].tobytes() # Encode the image as JPEG
  75. # Create the email
  76. message = MIMEMultipart()
  77. message["From"] = self.from_email
  78. message["To"] = self.to_email
  79. message["Subject"] = "Security Alert"
  80. # Add the text message body
  81. message_body = f"Ultralytics ALERT!!! {records} objects have been detected!!"
  82. message.attach(MIMEText(message_body))
  83. # Attach the image
  84. image_attachment = MIMEImage(img_bytes, name="ultralytics.jpg")
  85. message.attach(image_attachment)
  86. # Send the email
  87. try:
  88. self.server.send_message(message)
  89. LOGGER.info("โœ… Email sent successfully!")
  90. except Exception as e:
  91. LOGGER.error(f"Failed to send email: {e}")
  92. def process(self, im0):
  93. """
  94. Monitor the frame, process object detections, and trigger alerts if thresholds are exceeded.
  95. Args:
  96. im0 (numpy.ndarray): The input image or frame to be processed and annotated.
  97. Returns:
  98. (SolutionResults): Contains processed image `plot_im`, 'total_tracks' (total number of tracked objects) and
  99. 'email_sent' (whether an email alert was triggered).
  100. This method processes the input frame, extracts detections, annotates the frame with bounding boxes, and sends
  101. an email notification if the number of detected objects surpasses the specified threshold and an alert has not
  102. already been sent.
  103. Examples:
  104. >>> alarm = SecurityAlarm()
  105. >>> frame = cv2.imread("path/to/image.jpg")
  106. >>> results = alarm.process(frame)
  107. """
  108. self.extract_tracks(im0) # Extract tracks
  109. annotator = SolutionAnnotator(im0, line_width=self.line_width) # Initialize annotator
  110. # Iterate over bounding boxes and classes index
  111. for box, cls in zip(self.boxes, self.clss):
  112. # Draw bounding box
  113. annotator.box_label(box, label=self.names[cls], color=colors(cls, True))
  114. total_det = len(self.clss)
  115. if total_det > self.records and not self.email_sent: # Only send email if not sent before
  116. self.send_email(im0, total_det)
  117. self.email_sent = True
  118. plot_im = annotator.result()
  119. self.display_output(plot_im) # Display output with base class function
  120. # Return a SolutionResults
  121. return SolutionResults(plot_im=plot_im, total_tracks=len(self.track_ids), email_sent=self.email_sent)
Discard
Tip!

Press p or to see the previous file or, n or to see the next file