Exemple #1
0
def run_on_photo(photo_id):
    model = ObjectModel()
    sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
    from photonix.classifiers.runners import results_for_model_on_photo, get_or_create_tag
    photo, results = results_for_model_on_photo(model, photo_id)

    if photo:
        from django.utils import timezone
        from photonix.photos.models import PhotoTag
        photo.clear_tags(source='C', type='O')
        for result in results:
            tag = get_or_create_tag(library=photo.library,
                                    name=result['label'],
                                    type='O',
                                    source='C')
            PhotoTag(photo=photo,
                     tag=tag,
                     source='C',
                     confidence=result['score'],
                     significance=result['significance'],
                     position_x=result['x'],
                     position_y=result['y'],
                     size_x=result['width'],
                     size_y=result['height']).save()
        photo.classifier_object_completed_at = timezone.now()
        photo.classifier_object_version = getattr(model, 'version', 0)
        photo.save()

    return photo, results
Exemple #2
0
def run_on_photo(photo_id):
    model = StyleModel()
    sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
    from photonix.classifiers.runners import results_for_model_on_photo, get_or_create_tag
    photo, results = results_for_model_on_photo(model, photo_id)

    if photo:
        from django.utils import timezone
        from photonix.photos.models import PhotoTag
        photo.clear_tags(source='C', type='S')
        for name, score in results:
            tag = get_or_create_tag(library=photo.library,
                                    name=name,
                                    type='S',
                                    source='C')
            PhotoTag(photo=photo,
                     tag=tag,
                     source='C',
                     confidence=score,
                     significance=score).save()
        photo.classifier_style_completed_at = timezone.now()
        photo.classifier_style_version = getattr(model, 'version', 0)
        photo.save()

    return photo, results
Exemple #3
0
def run_on_photo(photo_id):
    model = LocationModel()
    sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
    from photonix.classifiers.runners import results_for_model_on_photo, get_or_create_tag
    photo, results = results_for_model_on_photo(model, photo_id)

    if photo and results['country']:
        from photonix.photos.models import PhotoTag
        photo.clear_tags(source='C', type='L')
        country_tag = get_or_create_tag(library=photo.library,
                                        name=results['country']['name'],
                                        type='L',
                                        source='C')
        PhotoTag(photo=photo,
                 tag=country_tag,
                 source='C',
                 confidence=1.0,
                 significance=1.0).save()
        if results['city']:
            city_tag = get_or_create_tag(library=photo.library,
                                         name=results['city']['name'],
                                         type='L',
                                         source='C',
                                         parent=country_tag)
            PhotoTag(photo=photo,
                     tag=city_tag,
                     source='C',
                     confidence=0.5,
                     significance=0.5).save()

    return photo, results
Exemple #4
0
def run_on_photo(photo_id):
    model = StyleModel()
    sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
    from photonix.classifiers.runners import results_for_model_on_photo, get_or_create_tag
    photo, results = results_for_model_on_photo(model, photo_id)

    if photo and results is not None:
        from photonix.photos.models import PhotoTag
        photo.clear_tags(source='C', type='S')
        for name, score in results:
            tag = get_or_create_tag(library=photo.library, name=name, type='S', source='C')
            PhotoTag(photo=photo, tag=tag, source='C', confidence=score, significance=score).save()

    return photo, results
Exemple #5
0
def run_on_photo(photo_id):
    sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
    from photonix.classifiers.runners import get_photo_by_any_type, results_for_model_on_photo, get_or_create_tag

    photo = get_photo_by_any_type(photo_id)
    model = FaceModel(library_id=photo and photo.library_id)

    # Detect all faces in an image
    photo, results = results_for_model_on_photo(model, photo_id)

    # Read image data so we can extract faces and create embeddings
    path = photo_id
    if photo:
        path = photo.base_image_path
        model.library_id = photo.library_id
    image_data = Image.open(path)

    # Loop over each face that was detected above
    for result in results:
        # Crop individual face + 30% extra in each direction
        box = result['box']
        face_image = model.crop(image_data, box)
        # Generate embedding with Facenet
        try:
            embedding = model.get_face_embedding(face_image)
            # Add it to the results
            result['embedding'] = embedding
            if photo:
                closest_tag, closest_distance = model.find_closest_face_tag(
                    embedding)
                if closest_tag:
                    print(f'Closest tag: {closest_tag}')
                    print(f'Closest distance: {closest_distance}')
                    result['closest_tag'] = closest_tag
                    result['closest_distance'] = closest_distance
        except ValueError:
            pass

    if photo:
        from django.utils import timezone
        from photonix.photos.models import Tag, PhotoTag

        photo.clear_tags(source='C', type='F')
        for result in results:
            # Use matched tag if within distance threshold
            if result.get('closest_distance', 999) < DISTANCE_THRESHOLD:
                tag = Tag.objects.get(id=result['closest_tag'],
                                      library=photo.library,
                                      type='F')
                print(f'MATCHED {tag.name}')

            # Otherwise create new tag
            else:
                while True:
                    random_name = f'Unknown person {randint(0, 999999):06d}'
                    try:
                        Tag.objects.get(library=photo.library,
                                        name=random_name,
                                        type='F',
                                        source='C')
                    except Tag.DoesNotExist:
                        tag = Tag(library=photo.library,
                                  name=random_name,
                                  type='F',
                                  source='C')
                        tag.save()
                        break

            x = (result['box'][0] +
                 (result['box'][2] / 2)) / photo.base_file.width
            y = (result['box'][1] +
                 (result['box'][3] / 2)) / photo.base_file.height
            width = result['box'][2] / photo.base_file.width
            height = result['box'][3] / photo.base_file.height
            score = result['confidence']

            extra_data = ''
            if 'embedding' in result:
                extra_data = json.dumps(
                    {'facenet_embedding': result['embedding']})

            PhotoTag(photo=photo,
                     tag=tag,
                     source='F',
                     confidence=score,
                     significance=score,
                     position_x=x,
                     position_y=y,
                     size_x=width,
                     size_y=height,
                     model_version=model.version,
                     retrained_model_version=model.retrained_version,
                     extra_data=extra_data).save()
        photo.classifier_color_completed_at = timezone.now()
        photo.classifier_color_version = getattr(model, 'version', 0)
        photo.save()

    print('Finished')

    return photo, results