pip install tensorflow를 설치하는 과정에서 아래와 같은 오류가 발생하면 레지스트리 정보를 수정해야 함

윈도우+R 누르면 뜨는 검색창에 regedit 레지스트리 편집기에서Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem 경로로 찾아서 들어가면 LongPathsEnabled (Type: REG_DWORD)라는 키가 오른쪽 탭에 보임 더블클릭 후 값 1로 설정, 그리고 다시 재설치 (안되면 재부팅)

Untitled

import streamlit as st
from pymongo import MongoClient
from PIL import Image
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions

# MongoDB에 연결
client = MongoClient('mongodb://localhost:27017/')

# 데이터베이스 선택
db = client['mydatabase_tensorflow']

# 컬렉션 선택
collection = db['uploaded_images']

def save_uploaded_image(image):
    image_data = {
        'image': image.tobytes(),
        'width': image.width,
        'height': image.height
    }
    collection.insert_one(image_data)

def classify_image(image):
    # 이미지 사이즈 조정
    resized_image = image.resize((224, 224))

    # 이미지 전처리
    preprocessed_image = preprocess_input(np.array(resized_image))

    # 이미지 분류 모델 로드
    model = tf.keras.applications.ResNet50(weights='imagenet')

    # 이미지 분류
    predictions = model.predict(np.expand_dims(preprocessed_image, axis=0))
    decoded_predictions = decode_predictions(predictions, top=3)[0]

    return decoded_predictions

# 이미지 업로드
st.header('이미지 분류')
uploaded_image = st.file_uploader('이미지 업로드', type=['jpg', 'jpeg', 'png'])
if uploaded_image is not None:
    img = Image.open(uploaded_image)
    st.image(img, caption='업로드한 이미지', use_column_width=True)
    save_uploaded_image(img)
    st.success('이미지가 저장되었습니다.')

    # 이미지 분류 결과 출력
    st.subheader('이미지 분류 결과')
    predictions = classify_image(img)
    for prediction in predictions:
        st.write(f'{prediction[1]} ({prediction[2]*100:.2f}%)')

Untitled