Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can anyone use C # to extract features from images? #82

Open
bbhxwl opened this issue Jan 22, 2025 · 0 comments
Open

Can anyone use C # to extract features from images? #82

bbhxwl opened this issue Jan 22, 2025 · 0 comments

Comments

@bbhxwl
Copy link

bbhxwl commented Jan 22, 2025

Can anyone use C # to extract features from images?
Can C # extract features by executing Python code similar to the following?

model = models.resnet50(pretrained=True)
model.eval()
model = torch.nn.Sequential(*list(model.children())[:-1])

preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])


def extract_features(image_path):
    if image_path.startswith('http://') or image_path.startswith('https://'):
        response = requests.get(image_path)
        img = Image.open(BytesIO(response.content)).convert('RGB')
    elif os.path.exists(image_path):  
        img = Image.open(image_path).convert('RGB')
    else:
        raise ValueError("Invalid image path or URL.")
    img_tensor = preprocess(img).unsqueeze(0)
    with torch.no_grad():
        features = model(img_tensor).numpy()
    return features.flatten()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant