def _collectPositves(self, imgPath, segments):
        """Collect all positive scoring segments

        Copy the images for all segments that score highter than > .5 to folder
        settings.positivesDir. These will be used to train future models.
        Also, copy the full image for reference.

        Args:
            imgPath (str): path name for main image
            segments (list): List of dictionary containing information on each segment
        """
        positiveSegments = 0
        ppath = pathlib.PurePath(imgPath)
        imgNameNoExt = str(os.path.splitext(ppath.name)[0])
        imgObj = None
        for segmentInfo in segments:
            if segmentInfo['score'] > .5:
                if settings.positivesDir:
                    postivesDateDir = goog_helper.dateSubDir(settings.positivesDir)
                    cropImgName = imgNameNoExt + '_Crop_' + segmentInfo['coordStr'] + '.jpg'
                    cropImgPath = os.path.join(str(ppath.parent), cropImgName)
                    if not imgObj:
                        imgObj = Image.open(imgPath)
                    cropped_img = imgObj.crop(segmentInfo['coords'])
                    cropped_img.save(cropImgPath, format='JPEG')
                    cropped_img.close()
                    goog_helper.copyFile(cropImgPath, postivesDateDir)
                    os.remove(cropImgPath)
                positiveSegments += 1

        if positiveSegments > 0:
            logging.warning('Found %d positives in image %s', positiveSegments, ppath.name)
        if imgObj:
            imgObj.close()
Beispiel #2
0
def alertFire(constants, cameraID, timestamp, imgPath, fireSegment):
    """Update Alerts DB and send alerts about given fire through all channels (pubsub, email, and sms)

    Args:
        constants (dict): "global" contants
        cameraID (str): camera name
        timestamp (int): time.time() value when image was taken
        imgPath: filepath of the original image
        fireSegment (dictionary): dictionary with information for the segment with fire/smoke
    """
    (croppedPath, annotatedPath) = genAnnotatedImages(constants, cameraID, timestamp, imgPath, fireSegment)

    # copy annotated image to publicly accessible settings.noticationsDir
    alertsDateDir = goog_helper.dateSubDir(settings.noticationsDir)
    croppedID = goog_helper.copyFile(croppedPath, alertsDateDir)
    annotatedID = goog_helper.copyFile(annotatedPath, alertsDateDir)
    # convert fileIDs into URLs usable by web UI
    croppedUrl = croppedID.replace('gs://', 'https://storage.googleapis.com/')
    annotatedUrl = annotatedID.replace('gs://', 'https://storage.googleapis.com/')

    dbManager = constants['dbManager']
    updateAlertsDB(dbManager, cameraID, timestamp, croppedUrl, annotatedUrl, fireSegment)
    pubsubFireNotification(cameraID, timestamp, croppedUrl, annotatedUrl, fireSegment)
    emailFireNotification(constants, cameraID, timestamp, imgPath, annotatedPath, fireSegment)
    smsFireNotification(dbManager, cameraID)

    # remove both temporary files
    os.remove(croppedPath)
    os.remove(annotatedPath)
    def _recordDetection(self, camera, timestamp, imgPath, fireSegment):
        """Record that a smoke/fire has been detected

        Record the detection with useful metrics in 'detections' table in SQL DB.
        Also, upload image file to google cloud

        Args:
            camera (str): camera name
            timestamp (int):
            imgPath: filepath of the image
            fireSegment (dictionary): dictionary with information for the segment with fire/smoke

        Returns:
            File IDs for the uploaded image file
        """
        logging.warning('Fire detected by camera %s, image %s, segment %s', camera, imgPath, str(fireSegment))
        # copy/upload file to detection dir
        detectionsDateDir = goog_helper.dateSubDir(settings.detectionsDir)
        fileID = goog_helper.copyFile(imgPath, detectionsDateDir)
        logging.warning('Uploaded to detections folder %s', fileID)

        dbRow = {
            'CameraName': camera,
            'Timestamp': timestamp,
            'MinX': fireSegment['MinX'],
            'MinY': fireSegment['MinY'],
            'MaxX': fireSegment['MaxX'],
            'MaxY': fireSegment['MaxY'],
            'Score': fireSegment['score'],
            'HistAvg': fireSegment['HistAvg'],
            'HistMax': fireSegment['HistMax'],
            'HistNumSamples': fireSegment['HistNumSamples'],
            'ImageID': fileID,
            'ModelId': self.modelId
        }
        self.dbManager.add_data('detections', dbRow)
        return fileID
def uploadMapGCS(latitude, longitude, locationid, zoom):
    imgPath = getMapLocal(latitude, longitude, locationid, zoom)
    logging.warning('uploading %s', imgPath)
    mapFileGCS = goog_helper.copyFile(imgPath, settings.mapsDir)
    os.remove(imgPath)
    return mapFileGCS