1
0
Fork 0

인페인팅

This commit is contained in:
Envy_PC 2024-06-18 18:56:21 +09:00
parent d7228e3ecc
commit 0420692e25
4 changed files with 49 additions and 38 deletions

View File

@ -24,22 +24,26 @@ logger = logging.getLogger('default_logger')
avg_price = 0 avg_price = 0
def detail_ai(driver, textarea, aicontents, combined_value, isTransSuccess): def detail_ai(driver, textarea, aicontents, combined_value, product_info, isTransSuccess):
try: try:
logger.debug("detail_ai 맞추기") logger.debug("detail_ai 맞추기")
aicontent_html = aicontents aicontent_html = aicontents
time.sleep(0.5) time.sleep(0.5)
current_value = textarea.get_attribute("data-value") current_value = product_info.current_value
logger.debug("current_value에서 기존 이미지 삭제") # current_value = textarea.get_attribute("data-value")
# logger.debug("current_value에서 기존 이미지 삭제")
# deleted_original_html_tags = original_html(current_value) # deleted_original_html_tags = original_html(current_value)
deleted_original_html_tags = ""
if isTransSuccess: if isTransSuccess:
deleted_original_html_tags = remove_resized_content(current_value) logger.debug(f"isTransSuccess : {isTransSuccess} 이므로 기존 이미지 삭제")
modified_html_tags = product_info.current_value
ai_value = combined_value + aicontent_html + deleted_original_html_tags # modified_html_tags = remove_resized_content(current_value)
else:
modified_html_tags = product_info.deleted_value
logger.debug(f"isTransSuccess : {isTransSuccess} 이므로 기존 이미지 유지")
ai_value = combined_value + aicontent_html + modified_html_tags
# ai_value = combined_value + aicontent_html + current_value # ai_value = combined_value + aicontent_html + current_value
# contents 변수의 값이 None이 아닐 때만 send_keys 메서드를 호출합니다. # contents 변수의 값이 None이 아닐 때만 send_keys 메서드를 호출합니다.
if aicontent_html is not None: if aicontent_html is not None:
@ -325,6 +329,22 @@ def find_delivery_fee(weight, delv_collection):
# product_info_text += f'네이버 상품의 최고 가격은 [{high_price}]' # product_info_text += f'네이버 상품의 최고 가격은 [{high_price}]'
# return product_info_text # return product_info_text
def remove_all_image_tags(current_html):
# BeautifulSoup 객체 생성
soup = BeautifulSoup(current_html, 'html.parser')
# 모든 img, figure, picture 태그를 찾아 제거
image_tags = soup.find_all(['img', 'figure', 'picture'])
for tag in image_tags:
tag.decompose() # 태그 자체를 문서에서 제거
# 수정된 HTML을 문자열로 변환하여 반환
deleted_html = str(soup)
return deleted_html
def remove_resized_content_ori(current_html): def remove_resized_content_ori(current_html):
# BeautifulSoup 객체 생성 # BeautifulSoup 객체 생성
soup = BeautifulSoup(current_html, 'html.parser') soup = BeautifulSoup(current_html, 'html.parser')
@ -350,13 +370,16 @@ def remove_resized_content(current_html):
# "img" 태그 중 src 속성이 'https://img.alicdn.com'로 시작하는 것을 찾아 모두 삭제 # "img" 태그 중 src 속성이 'https://img.alicdn.com'로 시작하는 것을 찾아 모두 삭제
images_to_remove = soup.find_all('img', src=lambda x: x and x.startswith('https://img.alicdn.com')) images_to_remove = soup.find_all('img', src=lambda x: x and x.startswith('https://img.alicdn.com'))
images_to_remove_percenty = soup.find_all('img', src=lambda x: x and x.startswith('https://file.percenty.co.kr'))
logger.debug(f"https://img.alicdn.com 의갯수 : [{len(images_to_remove)}] 개")
logger.debug(f"https://file.percenty.co.kr 의갯수 : [{len(images_to_remove_percenty)}] 개")
for img in images_to_remove: for img in images_to_remove:
img.decompose() # img 태그 자체를 문서에서 제거 img.decompose() # img 태그 자체를 문서에서 제거
# 수정된 HTML을 문자열로 변환하여 반환 # 수정된 HTML을 문자열로 변환하여 반환
modified_html = str(soup) modified_html = str(soup)
logger.debug("기존이미지 삭제 완료")
return modified_html return modified_html
def original_html(current_html): def original_html(current_html):
@ -677,12 +700,18 @@ def modify_detail_page(driver, product_info, gemini, translator, delv_collection
# # data-value 속성 값을 가져옵니다. # # data-value 속성 값을 가져옵니다.
current_value = textarea.get_attribute("data-value") current_value = textarea.get_attribute("data-value")
product_info.current_value = current_value product_info.current_value = current_value
logger.debug("현재 속성값 수집 완료") logger.debug("현재 속성값 수집")
# original_html_tags = original_html(current_value) # original_html_tags = original_html(current_value)
detail_images = fetch_image_urls(current_value) detail_images = fetch_image_urls(current_value)
product_info.detail_image_urls = detail_images product_info.detail_image_urls = detail_images
logger.debug("현재 속성값에서 이미지 URL 추출")
logger.debug(f"원본 이미지 URLs [{len(detail_images)}]개 수집 완료") logger.debug(f"원본 이미지 URLs [{len(detail_images)}]개 수집 완료")
deleted_value = remove_all_image_tags(current_value)
product_info.deleted_value = deleted_value
logger.debug("현재 속성값에서 이미지 제거")
# logger.debug(f"detail_images List : {detail_images}") # logger.debug(f"detail_images List : {detail_images}")
# logger.debug(f"현재 속성값 :{current_value}") # logger.debug(f"현재 속성값 :{current_value}")
# if not simpleMode: # 심플모드일 경우 상품카드 작성하지 않음 # if not simpleMode: # 심플모드일 경우 상품카드 작성하지 않음
@ -796,33 +825,11 @@ def modify_detail_page(driver, product_info, gemini, translator, delv_collection
logger.debug("====번역이미지 붙여넣기 완료=====") logger.debug("====번역이미지 붙여넣기 완료=====")
# try:
# logger.debug("aicontent_html 입력시작")
# html_insert_btn_xpath="//div[@id='productMainContentContainerId']/div/div/div[2]/div[2]/div/div[1]/div[2]/div/div/button[7]"
# click_element(driver, 'XPATH', html_insert_btn_xpath, 5, 'ac')
# logger.debug("html_insert_btn_xpath 클릭")
# html_insert_TEXTAREA_xpath="/html/body/div[7]/div/div[3]/div/div[2]/div[1]/div/div/div[2]/div[2]/div[2]/div/div[2]/div[2]/textarea"
# click_element(driver, 'XPATH', html_insert_TEXTAREA_xpath, 5, 'ac')
# html_insert_TEXTAREA_element = return_element(driver, 'XPATH', 'html_insert_TEXTAREA_xpath', 10)
# html_insert_TEXTAREA_element.send_keys(aicontents)
# logger.debug("html 요소로 전송")
# html_insert_ok_xpath="//div[@id='productMainContentContainerId']/div/div[2]/div[2]/div[2]/div/div[1]/div[2]/div/button[1]"
# click_element(driver, 'XPATH', html_insert_ok_xpath, 5, 'ac')
# logger.debug("html_insert_ok_xpath 클릭")
# logger.debug(f"AI 컨텐츠 입력완료")
# except Exception as e:
# logger.error(f"AI 컨텐츠 입력 중 에러발생 : {e}", exc_info=True)
detail_html(driver) detail_html(driver)
time.sleep(0.5) time.sleep(0.5)
textarea = detail_textarea(driver) textarea = detail_textarea(driver)
time.sleep(0.5) time.sleep(0.5)
detail_ai(driver, textarea, aicontents, combined_value, isTransSuccess) detail_ai(driver, textarea, aicontents, combined_value, product_info ,isTransSuccess)
time.sleep(0.5) time.sleep(0.5)
detail_html(driver) detail_html(driver)
time.sleep(0.5) time.sleep(0.5)

View File

@ -30,11 +30,14 @@ def modify_price_page(driver, product_infos):
time.sleep(2) # 페이지 로딩 대기. time.sleep(2) # 페이지 로딩 대기.
try: try:
plus_fee_xpath = "//div[@id='productMainContentContainerId']/div/div/div/div/div[2]/div/div/div[8]/div/div/div[3]/div/div/div/div/div[2]/input"
plus_fee_xpath="//div[@id='productMainContentContainerId']/div/div/div/div/div[2]/div/div/div[8]/div/div/div[3]/div/div/div/div/div[2]/input" plus_fee_css = "div#productMainContentContainerId div:nth-child(8) > div > div > div:nth-child(3) > div > div > div > div.ant-input-number.css-1li46mu.ant-input-number-outlined > div.ant-input-number-input-wrap > input"
plus_fee_element = return_element(driver, 'XPATH', plus_fee_xpath, 10) plus_fee_element = return_element(driver, 'XPATH', plus_fee_xpath, 10)
plus_fee_element_by_css = return_element(driver, 'CSS', plus_fee_css, 10)
plus_fee_text = plus_fee_element.text plus_fee_text = plus_fee_element.text
print(f"plus_fee_text : {plus_fee_text}")
plus_fee_by_css_text = plus_fee_element_by_css.text
print(f"plus_fee_by_css_text : {plus_fee_by_css_text}")
option_high_price = product_infos.option_high_price option_high_price = product_infos.option_high_price
option_low_price = product_infos.option_low_price option_low_price = product_infos.option_low_price

View File

@ -27,6 +27,7 @@ class ProductInfo:
self.ai_contents_mark = None # ai가 생성한 컨텐츠 결과 self.ai_contents_mark = None # ai가 생성한 컨텐츠 결과
self.current_value = None # 상세페이지의 현재 내용 self.current_value = None # 상세페이지의 현재 내용
self.deleted_value = None # 상세페이지의 이미지가 삭제된 내용
self.new_value = None # 상세페이지의 바뀐 내용 self.new_value = None # 상세페이지의 바뀐 내용
self.naver_products = [] # 네이버 파싱된 상품들 self.naver_products = [] # 네이버 파싱된 상품들

View File

@ -8,7 +8,7 @@ import numpy as np
def inpaint_text(image, detected_texts, logger, expansion=5): def inpaint_text(image, detected_texts, logger, expansion=5):
""" """
이미지 텍스트 제거를 위해 인페이팅 기법을 사용합니다. 이미지 텍스트 제거를 위해 인페이팅 기법을 사용합니다.
expansion 파라미터는 텍스트 주변의 마스크 확장 크기를 지정합니다. expansion 파라미터는 텍스트 주변의 마스크 확장 크기를 지정합니다.
""" """
# image = cv2.imread(image_path) # image = cv2.imread(image_path)