DEV Community

Ashutosh Mishra
Ashutosh Mishra

Posted on

Docker: giving error [Errno 2] No such file or directory

0

I'm building a simple app using: Dockerfile, readme.py and requirements.txt. When the Dockerfile builds I get the error: "No such file or directory". However, when I change the ADD to COPY in the Dockerfile it works. Do you know why this is? While running docker build command and when execution goes on last line of docker file i am getting this error

import numpy as np
import base64 
from PIL import Image
import io
import cv2
import skimage
import tensorflow as tf
def preprocessor(base64image,size):

    try:
        img_bytes = base64.b64decode(base64image)
        img = Image.open(io.BytesIO(img_bytes))
        img.verify()
        img.close()
        img = Image.open(io.BytesIO(img_bytes))
        img_arr = np.asarray(img)
        if len(img_arr.shape) == 2:
            img_arr = skimage.color.gray2rgb(img_arr)      
        if len(img_arr.shape) > 2 and img_arr.shape[2] == 4:
            img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGRA2BGR)
        orig_area = img_arr.shape[0]*img_arr.shape[1]
        resize_area = size[0]*size[1]

        if orig_area < resize_area:
            img_arr = cv2.resize(img_arr, size, interpolation = cv2.INTER_LINEAR)
        elif orig_area > resize_area:
            img_arr = cv2.resize(img_arr, size, interpolation = cv2.INTER_AREA)
        img_arr = img_arr.astype('float32') / 255.0
        img_arr = np.reshape(img_arr,[1]+list(img_arr.shape))

        return img_arr

    except:

        print('****** Error during image preprocessing ******')

#############################################

import os
from keras.models import load_model,save_model
import numpy as np
import tensorflow as tf

input_img_dir = '/app/Images for testing 2'
input_img_arr = []
image_name_arr = []
count = 0

for file in os.listdir(input_img_dir):
    file = file.lower()
    if (file.endswith('jpg')) or (file.endswith('jpeg'))  or (file.endswith('png') or (file.endswith('jfif'))):

        input_img_arr.append(os.path.join(input_img_dir,file))

with tf.device('/CPU:0'):
    model = load_model('/app/Final Models/crop_ResNet152V2_model_best_at_13_0.9923.h5')
    class_labels = ['Maize', 'Grapes', 'Cotton', 'Rice']

for image_path in input_img_arr:
    binary_image = open(image_path, "rb").read()#HERE IT GIVES ERROR
    base64image = base64.b64encode(binary_image)
    img = preprocessor(base64image,[512,512])
    prediction = model.predict(img)
    print(os.path.basename(image_path))
    print(prediction)
    preds = class_labels[np.argmax(prediction)]
    print(preds)
    print('\n')



####
Dockerfile
FROM python:3.10-slim-buster

WORKDIR /app
ADD . /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
RUN apt-get update && apt-get install -y python3-opencv
RUN pip install opencv-python

COPY . .

RUN [ "python3", "readme.py" ]

Enter fullscreen mode Exit fullscreen mode

Top comments (0)