def test_temperatures_when_data_is_not_present(self): """Test that temperature data can't be retrieved when there isn't data --negative test case""" tt = TemperatureTracker() result = tt.temperatures() self.assertEqual(result, [])
def test_get_stop_true(self): """Test that the stop value can be returned if the tracker has been stopped --positive test case""" tt = TemperatureTracker() tt.stop() self.assertIsNotNone(tt.get_stop())
def test_count_when_data_is_not_present(self): """Test that we can't get the count if there isn't temperature data -- negative test case""" temp_data = [] tt = TemperatureTracker() result = tt.count_from(temp_data) self.assertEqual(result, 0)
def test_count_when_data_present(self): """Test that we can get the count if there is temperature data -- positive test case""" temp_data = [(1.00, time.localtime()), (2.00, time.localtime()), (3.00, time.localtime()), (4.00, time.localtime())] tt = TemperatureTracker(temp_data) result = tt.count_from(temp_data) self.assertEqual(result, 4)
def test_minimum_all_different(self): """Test that the minimum temperature and time are calculated correctly when all input is the same""" temp_data = [(1.00, time.localtime()), (2.00, time.localtime()), (3.00, time.localtime()), (4.00, time.localtime())] tt = TemperatureTracker() result = tt.minimum_from(temp_data) self.assertEqual(result[0], 1.0) self.assertEqual(temp_data[0][1], result[1])
def test_average_all_different(self): """Test that the average temperature is calculated correctly when all input is different""" temp_data = [(1.00, time.localtime()), (2.00, time.localtime()), (3.00, time.localtime()), (4.00, time.localtime())] tt = TemperatureTracker() result = tt.average_from(temp_data) self.assertEqual(result, 2.5)
def test_maximum_all_same(self): """Test that the maximum temperature and time are calculated correctly when all input is the same""" temp_data = [(83.00, time.localtime()), (83.00, time.localtime()), (83.00, time.localtime()), (83.00, time.localtime()), (83.00, time.localtime())] tt = TemperatureTracker() result = tt.maximum_from(temp_data) self.assertEqual(result[0], 83.0) self.assertEqual(temp_data[3][1], result[1])
def test_maximum_all_different(self): """Test that the maximum temperature and time are calculated correctly when all input is different""" temp_data = [(92.00, time.localtime()), (102.00, time.localtime()), (83.00, time.localtime()), (104.30, time.localtime())] tt = TemperatureTracker() result = tt.maximum_from(temp_data) self.assertEqual(result[0], 104.3) self.assertEqual(temp_data[3][1], result[1])
def test_temperatures_when_data_present(self): """Test that temperature data can be retrieved when there is data --positive test case""" temp_data = [(1.00, time.localtime()), (2.00, time.localtime()), (3.00, time.localtime()), (4.00, time.localtime())] tt = TemperatureTracker(temp_data) result = tt.temperatures() for i in range(0, len(result)): self.assertEqual(result[i][0], temp_data[i][0]) self.assertEqual(result[i][1], temp_data[i][1])
def test_tracks_temperature_change(self): tracker = TemperatureTracker() mock_sensor = Mock() tracker.sensor = mock_sensor mock_sensor.configure_mock(**{'check_temperature.return_value': 12}) tracker.record_initial_temperature() mock_sensor.configure_mock(**{'check_temperature.return_value': 22}) self.assertEqual(10, tracker.find_temperature_change())
def test_average_all_same(self): """Test that the average temperature is calculated correctly when all input is the same""" temp_data = [(32.00, time.localtime()), (32.00, time.localtime()), (32.00, time.localtime()), (32.00, time.localtime())] tt = TemperatureTracker() result = tt.average_from(temp_data) self.assertEqual(result, 32.0) # test the regular average function the user will call tt = TemperatureTracker(temp_data) result = tt.average() self.assertEqual(result, 32.0)
def test_update_from(self): temp_data = [(1.00, time.localtime()), (2.00, time.localtime()), (3.00, time.localtime()), (4.00, time.localtime())] temp_data2 = [(1.00, time.localtime()), (2.00, time.localtime()), (3.00, time.localtime()), (4.00, time.localtime()), (6.00, time.localtime())] tt = TemperatureTracker(temp_data) tt.update_from((5.00, time.localtime()), temp_data) self.assertEqual(5, len(tt.temperatures())) # test overwriting the list with an empty list and appending a new reading tt.update_from((5.00, time.localtime()), []) self.assertEqual(1, len(tt.temperatures())) tt.update_from((5.00, time.localtime()), temp_data2) self.assertEqual(6, len(tt.temperatures()))
def test_get_stop_false(self): """Test that the stop value cannot be returned if the tracker hasn't stopped --negative test case""" tt = TemperatureTracker() self.assertIsNone(tt.get_stop())
def test_tracks_temperature_change(self, mock): tracker = TemperatureTracker() tracker.record_initial_temperature() self.assertEqual(10, tracker.find_temperature_change())
def main(): # if you would like to test an additional model, add one to the list below: models = ["alwaysai/mobilenet_ssd", "alwaysai/ssd_inception_v2_coco_2018_01_28"] # if you've added a model, add a new color in as a list of tuples in BGR format # to make visualization easier (e.g. [(B, G, R)]). colors = [[(66, 68, 179)], [(50, 227, 62)]] detectors = [] # initialize a list to hold temperature data TEMP_DATA = [] temperature_tracker = TemperatureTracker(TEMP_DATA) # load all the models (creates a new object detector for each model) for model in models: # start up a first object detection model obj_detect = edgeiq.ObjectDetection(model) obj_detect.load(engine=edgeiq.Engine.DNN) # track the generated object detection items by storing them in detectors detectors.append(obj_detect) # print the details of each model to the console print("Model:\n{}\n".format(obj_detect.model_id)) print("Engine: {}".format(obj_detect.engine)) print("Accelerator: {}\n".format(obj_detect.accelerator)) print("Labels:\n{}\n".format(obj_detect.labels)) fps = edgeiq.FPS() try: with edgeiq.WebcamVideoStream(cam=0) as video_stream, \ edgeiq.Streamer() as streamer: # Allow Webcam to warm up time.sleep(2.0) fps.start() # start the temperature tracker temperature_tracker.start() # loop detection while True: frame = video_stream.read() text = [""] # gather data from the all the detectors for i in range(0, len(detectors)): results = detectors[i].detect_objects( frame, confidence_level=.5) object_frame = edgeiq.markup_image( frame, results.predictions, show_labels=False, colors=colors[i]) # for the first frame, overwrite the input feed if i == 0: display_frame = object_frame else: # otherwise, append the new marked-up frame to the previous one display_frame = numpy.concatenate( (object_frame, display_frame)) # append each prediction for prediction in results.predictions: text.append("Model {} detects {}: {:2.2f}% (inference time: {:1.2f})".format(detectors[i].model_id, prediction.label, prediction.confidence * 100, results.duration)) # get an instance of the cpu temperature temperature_tracker.update() # send the image frame and the predictions for both # prediction models to the output stream streamer.send_data(display_frame, text) fps.update() if streamer.check_exit(): break finally: fps.stop() temperature_tracker.stop() print("elapsed time: {:.2f}".format(fps.get_elapsed_seconds())) print("approx. FPS: {:.2f}".format(fps.compute_fps())) summary = temperature_tracker.summary() print(summary) print(*TEMP_DATA) print("Program Ending")
def main(): obj_detect = edgeiq.ObjectDetection("alwaysai/mobilenet_ssd") obj_detect.load(engine=edgeiq.Engine.DNN) print("Loaded model:\n{}\n".format(obj_detect.model_id)) print("Engine: {}".format(obj_detect.engine)) print("Accelerator: {}\n".format(obj_detect.accelerator)) print("Labels:\n{}\n".format(obj_detect.labels)) temperature_tracker = TemperatureTracker() fps = edgeiq.FPS() try: with edgeiq.WebcamVideoStream(cam=0) as video_stream, \ edgeiq.Streamer() as streamer: # Allow Webcam to warm up time.sleep(2.0) fps.start() # start the temperature tracker temperature_tracker.start() # loop detection while True: frame = video_stream.read() results = obj_detect.detect_objects(frame, confidence_level=.5) frame = edgeiq.markup_image(frame, results.predictions, colors=obj_detect.colors) # Generate text to display on streamer text = ["Model: {}".format(obj_detect.model_id)] text.append("Inference time: {:1.3f} s".format( results.duration)) text.append("Objects:") for prediction in results.predictions: text.append("{}: {:2.2f}%".format( prediction.label, prediction.confidence * 100)) # get an instance of the cpu temperature temperature_tracker.update() # gather the current temperature and timestamp and print it now = temperature_tracker.now() # log block showing current temperature print( str(now[0]) + " " + time.strftime('%Y-%m-%d %H:%M:%S', now[1])) # details whether the temperature is safe for a Raspberry Pi 4 if now[0] < temperature_tracker.MAX_TEMP_RASP4: print("Temperature is safe") else: print("You should shut down") streamer.send_data(frame, text) fps.update() # exit program if maximum safe temp has been reached if now[0] >= temperature_tracker.MAX_TEMP_RASP4: print("Maximum safe temperature reached, stopping program") break if streamer.check_exit(): break finally: fps.stop() # stop the temperature tracker temperature_tracker.stop() # print summary details for inference time print("elapsed time: {:.2f}".format(fps.get_elapsed_seconds())) print("approx. FPS: {:.2f}".format(fps.compute_fps())) # print summary details for the temperature tracker summary = temperature_tracker.summary() print(summary) print("Program Ending")