Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-lightly committed Nov 10, 2023
1 parent cd87e3b commit 7016386
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
14 changes: 6 additions & 8 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
from pathlib import Path

from labelformat.formats import LightlyObjectDetectionInput
from labelformat.formats import YOLOv8ObjectDetectionInput

from lightly_insights import analyze, present


def main() -> None:
# Analyze an image folder.
image_folder = Path("/Users/michal/datasets/aquarium_predictions")
image_folder = Path("/Users/michal/datasets/aquarium.v2-release.yolov8/test/images")
image_analysis = analyze.analyze_images(image_folder=image_folder)

# Analyze object detections.
label_folder = Path(
"/Users/michal/datasets/aquarium_predictions/.lightly/predictions/object-detection"
)
label_input = LightlyObjectDetectionInput(
input_folder=label_folder,
images_rel_path="../../..",
input_file = Path("/Users/michal/datasets/aquarium.v2-release.yolov8/data.yaml")
label_input = YOLOv8ObjectDetectionInput(
input_file=input_file,
input_split="test",
)
od_analysis = analyze.analyze_object_detections(label_input=label_input)

Expand Down
31 changes: 31 additions & 0 deletions example_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from pathlib import Path

from labelformat.formats import PascalVOCObjectDetectionInput

from lightly_insights import analyze, present


def main() -> None:
# Analyze an image folder.
image_folder = Path("/Users/michal/datasets/VOC2007/JPEGImages")
image_analysis = analyze.analyze_images(image_folder=image_folder)

# Analyze object detections.
label_folder = Path("/Users/michal/datasets/VOC2007/Annotations")
label_input = PascalVOCObjectDetectionInput(
input_folder=label_folder,
category_names="person,bird,cat,cow,dog,horse,sheep,aeroplane,bicycle,boat,bus,car,motorbike,train,bottle,chair,diningtable,pottedplant,sofa,tvmonitor",
)
od_analysis = analyze.analyze_object_detections(label_input=label_input)

# Create HTML report.
output_folder = Path("/Users/michal/tmp/lightly_insights_output_2")
present.create_html_report(
output_folder=output_folder,
image_analysis=image_analysis,
od_analysis=od_analysis,
)


if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion src/lightly_insights/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import tqdm
from labelformat.model.object_detection import ObjectDetectionInput
from numpy.typing import NDArray
from PIL import Image

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -44,7 +45,7 @@ class ClassAnalysis:
objects_per_image: Counter[int]
object_sizes_abs: List[Tuple[float, float]]
object_sizes_rel: List[Tuple[float, float]]
heatmap: np.ndarray
heatmap: NDArray[np.float_]

sample_filenames: List[str]

Expand Down
3 changes: 2 additions & 1 deletion src/lightly_insights/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator
from numpy.typing import NDArray

from lightly_insights.analyze import ClassAnalysis

Expand Down Expand Up @@ -278,7 +279,7 @@ def _histogram(

def _heatmap(
output_file: Path,
heatmap: np.ndarray,
heatmap: NDArray[np.float_],
) -> None:
# Image size plot.
fig = plt.figure(figsize=(6, 6))
Expand Down
2 changes: 1 addition & 1 deletion src/lightly_insights/present.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def create_html_report(
report_data = dict(
image_analysis=image_analysis,
object_detection_analysis=od_analysis,
date_generated=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
image_insights=image_insights,
object_detection_insights=object_detection_insights,
filename_insights=filename_insights,
date_generated=datetime.now().astimezone().strftime("%Y-%m-%d %H:%M:%S %Z"),
)

# Setup Jinja2 environment
Expand Down
20 changes: 12 additions & 8 deletions src/lightly_insights/templates/report.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ <h4 class="card-header">Image Size Insights</h4>
<div class="card border-primary">
<h4 class="card-header">Object Detection Insights</h4>
<div class="card-body">
<h4 class="card-title">{{ object_detection_analysis.total.num_objects }} Objects</h4>
<h4 class="card-title">{{ object_detection_analysis.total.num_objects }} Objects of {{ object_detection_insights.num_classes }} Classes</h4>

<div class="col-lg-6">

Expand Down Expand Up @@ -315,7 +315,6 @@ <h4>{{ class.num_objects }} Objects of Class "{{ class.class_name }}"</h4>
{% if class.num_objects > 0 %}

<div class="col-lg-6">

<table class="table table-hover mt-4">
<tbody>
<tr>
Expand All @@ -328,15 +327,10 @@ <h4>{{ class.num_objects }} Objects of Class "{{ class.class_name }}"</h4>
</tr>
</tbody>
</table>

</div>


{% with plots=object_detection_insights.class_plots[class_id] %}
{% include 'plots.html' %}
{% endwith %}

<h4>Sample Images</h4>
<h4 class="mt-4">Sample Images</h4>

<div class="row">
{% for filename in class.sample_filenames %}
Expand All @@ -349,6 +343,12 @@ <h4>Sample Images</h4>
{% endfor %}
</div>

<h4 class="mt-4">Statistics</h4>

{% with plots=object_detection_insights.class_plots[class_id] %}
{% include 'plots.html' %}
{% endwith %}

{% endif %}


Expand All @@ -361,6 +361,10 @@ <h4>Sample Images</h4>
</div>
</div>

<!-- Footer -->
<div id="section-grid-layout" class="my-5">
<p class="text-center text-secondary">Generated with <a href="https://github.com/lightly-ai/lightly-insights">Lightly Insights</a>.</p>
</div>

</div>

Expand Down

0 comments on commit 7016386

Please sign in to comment.