detect floorplan using asyncio
This commit is contained in:
parent
68cc70bd11
commit
b1e0ed170b
3 changed files with 22 additions and 9 deletions
|
|
@ -1,15 +1,25 @@
|
||||||
|
import asyncio
|
||||||
import pathlib
|
import pathlib
|
||||||
from data_access import Listing
|
from data_access import Listing
|
||||||
from tqdm import tqdm
|
from tqdm.asyncio import tqdm
|
||||||
|
import multiprocessing
|
||||||
|
|
||||||
|
|
||||||
def detect_floorplan(listing_paths: list[str]):
|
async def detect_floorplan(listing_paths: list[str]):
|
||||||
listings = Listing.get_all_listings(listing_paths)
|
listings = Listing.get_all_listings(listing_paths)
|
||||||
|
cpu_count = multiprocessing.cpu_count() / 4
|
||||||
|
semaphore = asyncio.Semaphore(cpu_count)
|
||||||
|
|
||||||
for listing in tqdm(listings):
|
await tqdm.gather(
|
||||||
tqdm.write(str(listing.identifier))
|
*[_detect_floorplan_with_semaphore(listing, semaphore) for listing in listings]
|
||||||
# listing.calculate_sqm_model() # using google/deplot model. Too slow, rather use tesseract
|
)
|
||||||
listing.calculate_sqm_ocr(recalculate=False)
|
|
||||||
|
|
||||||
|
async def _detect_floorplan_with_semaphore(
|
||||||
|
listing: Listing, semaphore: asyncio.Semaphore
|
||||||
|
):
|
||||||
|
async with semaphore:
|
||||||
|
return await listing.calculate_sqm_ocr(recalculate=False)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import asyncio
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
@ -135,13 +136,15 @@ class Listing:
|
||||||
) # filter out Nones
|
) # filter out Nones
|
||||||
return max_sqm
|
return max_sqm
|
||||||
|
|
||||||
def calculate_sqm_ocr(self, recalculate=True):
|
async def calculate_sqm_ocr(self, recalculate=True):
|
||||||
if not recalculate and self.path_floorplan_ocr_json().exists():
|
if not recalculate and self.path_floorplan_ocr_json().exists():
|
||||||
return
|
return
|
||||||
|
|
||||||
objs = []
|
objs = []
|
||||||
for floorplan_path in self.list_floorplans():
|
for floorplan_path in self.list_floorplans():
|
||||||
estimated_sqm, model_output = floorplan.calculate_ocr(floorplan_path)
|
estimated_sqm, model_output = await asyncio.to_thread(
|
||||||
|
floorplan.calculate_ocr, floorplan_path
|
||||||
|
)
|
||||||
objs.append(
|
objs.append(
|
||||||
{
|
{
|
||||||
"floorplan_path": str(floorplan_path),
|
"floorplan_path": str(floorplan_path),
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,7 @@ def detect_floorplan(ctx: click.core.Context):
|
||||||
data_dir = ctx.obj['data_dir']
|
data_dir = ctx.obj['data_dir']
|
||||||
click.echo(f'Running detect_floorplan in {data_dir}')
|
click.echo(f'Running detect_floorplan in {data_dir}')
|
||||||
listing_paths = sorted(list(pathlib.Path(data_dir).glob("*/listing.json")))
|
listing_paths = sorted(list(pathlib.Path(data_dir).glob("*/listing.json")))
|
||||||
detect_floorplan_module.detect_floorplan(listing_paths)
|
asyncio.run(detect_floorplan_module.detect_floorplan(listing_paths))
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue