Skip to content
Snippets Groups Projects
Unverified Commit 300fe9b9 authored by Anton Tetov Johansson's avatar Anton Tetov Johansson
Browse files

reworked download py from gripper and cleanup

parent 511635ea
Branches
Tags
No related merge requests found
......@@ -145,4 +145,4 @@ dmypy.json
cython_debug/
weights/yolov3-veges_best.weights
weights/yolov3-vattenhallen_best.weights
src/creds.py
creds.py
img/WIN_20211103_21_16_18_Pro.jpg

367 KiB

3 1055.2865 148.5889 443.2753 73.4710 99.5100
1 188.5453 22.3466 85.2053 49.0357 99.9200
3 1048.2261 134.2752 271.3041 76.3464 99.9600
350 700 -100
\ No newline at end of file
This folder is to store all the photos of the planting bed,
and is supposed to be cleaned each time after detection.
\ No newline at end of file
"""Download images from Farmbot instance."""
import argparse
import os
from pathlib import Path
from typing import List
import requests
import utils.creds as creds
# note: download only returns 100 at a time!
# note: we are currently ignoreing placeholders
DEBUG_SKIP_DELETE_FILES = True
IMG_FILE_SUFFIX = ".jpg"
REQUEST_HEADERS = {
"Authorization": "Bearer " + creds.token,
"content-type": "application/json",
}
def download_images(directory: os.PathLike, delete_after=False) -> List[str]:
"""Download all images on server, optionally deleting after download.
Parameters
----------
directory
directory to store images in
Raises
------
RuntimeError
If the server gives a bad response (not 200).
Returns
-------
list of str
List of filepaths with downloaded images
"""
response = requests.get("https://my.farmbot.io/api/images", headers=REQUEST_HEADERS)
json_response = response.json()
if response.status_code != 200:
raise RuntimeError(f"Got status code {response.status_code}.")
print(json_response)
print(f"Got a response containing {len(json_response)}")
if len(json_response) < 1:
return []
img_paths = []
for img_dict in json_response:
if "placehold.it" in img_dict["attachment_url"]:
print("IGNORE! placeholder", img_dict["id"])
continue
server_path: str = img_dict["meta"]["name"]
if not server_path.startswith("/tmp/images"):
print("meta name does not start with /tmp/images")
continue
filename = Path(server_path).stem + IMG_FILE_SUFFIX
filepath = Path(directory) / filename
print(">", filepath)
# download image from google storage and save locally
if filepath.exists():
print("File exists, skipping")
continue
img_req = requests.get(img_dict["attachment_url"], allow_redirects=True)
with filepath.open(mode="wb") as fp:
fp.write(img_req.content)
img_paths.append(filepath)
# post delete from cloud storage
if delete_after:
requests.delete(
f"https://my.farmbot.io/api/images/{img_dict['id']}",
headers=REQUEST_HEADERS,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Farmbot YOLO image downloader")
parser.add_argument(
"download_dir", type=Path, help="Directory to download images too."
)
parser.add_argument("-d", "--delete", action="store_true")
args = parser.parse_args()
download_images(args.download_dir, delete_after=args.delete)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment