Skip to main content

Segment and count object using SAM

Project description

How to use the package

Example usecase:

import os
import cv2
from tqdm import tqdm
from segcount.segment_and_count import ObjectCounter

DEVICE = 'cpu' # or 'cuda:0'
SAM_MODEL_TYPE = 'vit_h'

def run(img_folder, output_object = 'object_detected', output_planks = 'oplanks_detected'):
    os.makedirs(os.path.join(img_folder, output_object), exist_ok=True)
    os.makedirs(os.path.join(img_folder, output_planks), exist_ok=True)

    all_imgs = [file for file in os.listdir(img_folder) if file.split('.')[-1] in ['jpg', 'png']]
    counter = ObjectCounter(device=DEVICE, sam_model_type=SAM_MODEL_TYPE)

    for img_file in tqdm(all_imgs):
        path_read = os.path.join(img_folder, img_file)
        path_save = os.path.join(img_folder, output_object, img_file) + '.output_object.jpg'

        img = cv2.imread(path_read)
        obj = counter.detect_object(img)
        cv2.imwrite(path_save, obj)

        print(counter.count_planks(obj))

if __name__ == '__main__':
    img_folder = '<path to your images folder>'
    run(img_folder)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

segcount-0.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

segcount-0.0.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file segcount-0.0.1.tar.gz.

File metadata

  • Download URL: segcount-0.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.10

File hashes

Hashes for segcount-0.0.1.tar.gz
Algorithm Hash digest
SHA256 202a828efabd5114bfa157c6250c7716398c638e290e9b97b2a8d1eafdd79d55
MD5 5d7d985557c9caf283f334679a6db554
BLAKE2b-256 2459f4b02550a940151934bdc3bfa0042598b73677847688864e471087b8f478

See more details on using hashes here.

File details

Details for the file segcount-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: segcount-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.10

File hashes

Hashes for segcount-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7d0d91a34af8b1168e109bc3e0623dfab46d7371bc521b7fc7b0fc9083d9f806
MD5 2525cb33828efb9ebd2a3de65a62af4e
BLAKE2b-256 00d68b8ff74f8334d93ac7173c4e73ad8361e61f9644075d2984a313e8b4ce08

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page