|
from object_detection import ObjectDetector |
|
import os |
|
|
|
def detect_objects_for_image(image_name, detector): |
|
|
|
if os.path.exists(image_path): |
|
image = detector.process_image(image_path) |
|
detected_objects_str, _ = detector.detect_objects(image) |
|
return detected_objects_str |
|
else: |
|
return "Image not found" |
|
|
|
def add_detected_objects_to_dataframe(df, image_directory, detector): |
|
""" |
|
Adds a column to the DataFrame with detected objects for each image specified in the 'image_name' column. |
|
|
|
Parameters: |
|
df (pd.DataFrame): DataFrame containing a column 'image_name' with image filenames. |
|
image_directory (str): Path to the directory containing images. |
|
detector (ObjectDetector): An instance of the ObjectDetector class. |
|
|
|
Returns: |
|
pd.DataFrame: The original DataFrame with an additional column 'detected_objects'. |
|
""" |
|
|
|
|
|
if 'image_name' not in df.columns: |
|
raise ValueError("DataFrame must contain an 'image_name' column.") |
|
|
|
image_path = os.path.join(image_directory, image_name) |
|
|
|
|
|
|
|
|
|
|
|
df['detected_objects'] = df['image_name'].apply(detect_objects_for_image) |
|
|
|
return df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|