Download image_utils.py from XAI/VisualCorrespondenceHumanStudy: direct link, hf CLI and curl.
- Browser
- Download file 3.92 kB
-
https://huggingface.co/spaces/XAI/VisualCorrespondenceHumanStudy/resolve/main/image_utils.py
- Command line
-
hf download hf://spaces/XAI/VisualCorrespondenceHumanStudy/image_utils.py
-
curl -L -o image_utils.py https://huggingface.co/spaces/XAI/VisualCorrespondenceHumanStudy/resolve/main/image_utils.py
3.92 kB
| import json | |
| import os | |
| import pickle | |
| import random | |
| from glob import glob | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| import seaborn as sns | |
| import streamlit as st | |
| from PIL import Image | |
| def load_query(image_path): | |
| image = Image.open(image_path) | |
| width, height = image.size | |
| new_width = width | |
| new_height = height | |
| left = (width - new_width) / 2 | |
| top = (height - new_height) / 2 | |
| right = (width + new_width) / 2 | |
| bottom = (height + new_height) / 2 | |
| # Crop the center of the image | |
| cropped_image = image.crop( | |
| (left + 75, top + 145, right - 2025, bottom - (2915 + 25 + 10)) | |
| ).resize((300, 300)) | |
| return cropped_image | |
| # CHM ############################################################################ | |
| def load_chm_nns(image_path): | |
| image = Image.open(image_path) | |
| width, height = image.size | |
| new_width = width | |
| new_height = height | |
| left = (width - new_width) / 2 | |
| top = (height - new_height) / 2 | |
| right = (width + new_width) / 2 | |
| bottom = (height + new_height) / 2 | |
| # Crop the center of the image | |
| cropped_image = image.crop( | |
| (left + 475, top + 140, right - 280, bottom - (2920 + 25 + 10)) | |
| ) | |
| return cropped_image | |
| def load_chm_corrs(image_path): | |
| image = Image.open(image_path) | |
| width, height = image.size | |
| new_width = width | |
| new_height = height | |
| left = (width - new_width) / 2 | |
| top = (height - new_height) / 2 | |
| right = (width + new_width) / 2 | |
| bottom = (height + new_height) / 2 | |
| # Crop the center of the image | |
| cropped_image = image.crop( | |
| (left + 475, top + 875, right - 280, bottom - (1810 + 25 + 10)) | |
| ) | |
| return cropped_image | |
| # CHM ############################################################################ | |
| # KNN ############################################################################ | |
| def load_knn_nns(image_path): | |
| image = Image.open(image_path) | |
| width, height = image.size | |
| new_width = width | |
| new_height = height | |
| left = (width - new_width) / 2 | |
| top = (height - new_height) / 2 | |
| right = (width + new_width) / 2 | |
| bottom = (height + new_height) / 2 | |
| # Crop the center of the image | |
| cropped_image = image.crop( | |
| (left + 475, top + 510, right - 280, bottom - (2550 + 25 + 10)) | |
| ) | |
| return cropped_image | |
| # KNN ############################################################################ | |
| # EMD ############################################################################ | |
| def load_emd_nns(image_path): | |
| image = Image.open(image_path) | |
| width, height = image.size | |
| new_width = width | |
| new_height = height | |
| left = (width - new_width) / 2 | |
| top = (height - new_height) / 2 | |
| right = (width + new_width) / 2 | |
| bottom = (height + new_height) / 2 | |
| # Crop the center of the image | |
| cropped_image = image.crop( | |
| (left + 10, top + 2075, right - 420, bottom - (925 + 25 + 10)) | |
| ) | |
| return cropped_image | |
| def load_emd_corrs(image_path): | |
| image = Image.open(image_path) | |
| width, height = image.size | |
| new_width = width | |
| new_height = height | |
| left = (width - new_width) / 2 | |
| top = (height - new_height) / 2 | |
| right = (width + new_width) / 2 | |
| bottom = (height + new_height) / 2 | |
| # Crop the center of the image | |
| cropped_image = image.crop((left + 10, top + 2500, right - 20, bottom)) | |
| return cropped_image | |
| # EMD ############################################################################ | |
| def convert_df(df): | |
| return df.to_csv().encode("utf-8") | |