Exemplo n.º 1
0
def post_url(request: Request, files: str = Form(...)):
    print(type(files))
    try:
        resp = requests.get(files, stream=True)

        if not "image" in resp.headers['content-type']:
            del resp
            raise HTTPException(status_code=404, detail="Not an image file")

        a = urlparse(files)
        name = basename(a.path)
        with open(cwd + f"\\static\\userFiles\\{name}", 'wb') as local_file:
            resp.raw.decode_content = True
            copyfileobj(resp.raw, local_file)

        print(f"name => {name.split('.')[0]} ")
        segment(cwd + f"\\static\\userFiles\\{name}", f"{name.split('.')[0]}")

        # if isfile(url):
        # 	remove(url)
        name = name.split('.')[0]

    except:
        return {'error': "Something went wrong while segmeting url"}
    # return files

    return templates.TemplateResponse(
        "segment_result.html", {
            "request": request,
            "code": f"/static/codes.txt",
            "image": f"/static/segment/{name}.png",
            "file": f"/static/segment/{name}.pt"
        })
Exemplo n.º 2
0
def post_url(request: Request, files: UploadFile = File(...)):
    print(type(files))

    try:
        if not "image" in str(files.content_type):
            raise HTTPException(status_code=404, detail="Not an image file")
    except:
        raise HTTPException(status_code=404, detail="Not an image file")

    with open(cwd + f"\\static\\userFiles\\{files.filename}", "wb") as buffer:
        copyfileobj(files.file, buffer)

    name = files.filename.split(".")[0]
    segment(cwd + f"\\static\\userFiles\\{files.filename}", f'{name}')

    # if isfile(url):
    # 	remove(url)
    # return files

    return templates.TemplateResponse(
        "segment_result.html", {
            "request": request,
            "code": f"/static/codes.txt",
            "image": f"/static/segment/{name}.png",
            "file": f"/static/segment/{name}.pt"
        })
Exemplo n.º 3
0
def post_url(request: Request, files: str = Form(...)):
    print(type(files))
    try:
        resp = requests.get(files, stream=True)

        if not "image" in resp.headers['content-type']:
            del resp
            raise HTTPException(status_code=404, detail="Not an image file")

        a = urlparse(files)
        name = basename(a.path)
        with open(cwd + f"\\static\\userFiles\\{name}", 'wb') as local_file:
            resp.raw.decode_content = True
            copyfileobj(resp.raw, local_file)

        pred, url = classify(cwd + f"\\static\\userFiles\\{name}")

        # if isfile(url):
        # 	remove(url)

    except Exception as e:
        return {"Something went wrong while classifying url": f'{e}'}
    # return files

    return templates.TemplateResponse(
        "classify_result.html", {
            "request": request,
            "image": f"/static/userFiles/{name}",
            "file": name,
            "prediction": pred
        })
Exemplo n.º 4
0
def post_image(request: Request, files: UploadFile = File(...)):
    try:
        if not "image" in str(files.content_type):
            raise HTTPException(status_code=404, detail="Not an image file")
    except:
        raise HTTPException(status_code=404, detail="Not an image file")

    with open(cwd + f"\\static\\userFiles\\{files.filename}", "wb") as buffer:
        copyfileobj(files.file, buffer)

    pred, url = classify(cwd + f"\\static\\userFiles\\{files.filename}")

    # if isfile(url):
    # 	remove(url)
    # print(f"pred => {pred}")
    # print(f"url => {url}")
    return templates.TemplateResponse(
        "classify_result.html", {
            "request": request,
            "image": f"/static/userFiles/{files.filename}",
            "file": files.filename,
            "prediction": pred
        })
Exemplo n.º 5
0
def read_url(request: Request):
    return templates.TemplateResponse("segment_url_form.html", {
        "request": request,
        "code": "/static/codes.txt"
    })
Exemplo n.º 6
0
def read_url(request: Request):
    return templates.TemplateResponse("classify_url_form.html",
                                      {"request": request})
Exemplo n.º 7
0
def read_image(request: Request):
    return templates.TemplateResponse("classification_form.html",
                                      {"request": request})
Exemplo n.º 8
0
def read_item_with_id(request: Request, id: int, q: Optional[str] = None):
    return templates.TemplateResponse("item.html", {
        "request": request,
        "id": id,
        'q': q
    })
Exemplo n.º 9
0
def read_item(request: Request, q: Optional[str] = None):
    return templates.TemplateResponse("item.html", {
        "request": request,
        "id": "None",
        'q': q
    })
Exemplo n.º 10
0
def home(request: Request):
    content = r"go to /item/{id}?q={query+string}"
    return templates.TemplateResponse("home.html", {
        "request": request,
        "content": content
    })