MCPcopy Create free account
hub / github.com/chenfei-wu/TaskMatrix / InfinityOutPainting

Class InfinityOutPainting

visual_chatgpt.py:1184–1279  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1182 return update_image
1183
1184class InfinityOutPainting:
1185 template_model = True # Add this line to show this is a template model.
1186 def __init__(self, ImageCaptioning, Inpainting, VisualQuestionAnswering):
1187 self.llm = OpenAI(temperature=0)
1188 self.ImageCaption = ImageCaptioning
1189 self.inpaint = Inpainting
1190 self.ImageVQA = VisualQuestionAnswering
1191 self.a_prompt = 'best quality, extremely detailed'
1192 self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
1193 'fewer digits, cropped, worst quality, low quality'
1194
1195 def get_BLIP_vqa(self, image, question):
1196 inputs = self.ImageVQA.processor(image, question, return_tensors="pt").to(self.ImageVQA.device,
1197 self.ImageVQA.torch_dtype)
1198 out = self.ImageVQA.model.generate(**inputs)
1199 answer = self.ImageVQA.processor.decode(out[0], skip_special_tokens=True)
1200 print(f"\nProcessed VisualQuestionAnswering, Input Question: {question}, Output Answer: {answer}")
1201 return answer
1202
1203 def get_BLIP_caption(self, image):
1204 inputs = self.ImageCaption.processor(image, return_tensors="pt").to(self.ImageCaption.device,
1205 self.ImageCaption.torch_dtype)
1206 out = self.ImageCaption.model.generate(**inputs)
1207 BLIP_caption = self.ImageCaption.processor.decode(out[0], skip_special_tokens=True)
1208 return BLIP_caption
1209
1210 def check_prompt(self, prompt):
1211 check = f"Here is a paragraph with adjectives. " \
1212 f"{prompt} " \
1213 f"Please change all plural forms in the adjectives to singular forms. "
1214 return self.llm(check)
1215
1216 def get_imagine_caption(self, image, imagine):
1217 BLIP_caption = self.get_BLIP_caption(image)
1218 background_color = self.get_BLIP_vqa(image, 'what is the background color of this image')
1219 style = self.get_BLIP_vqa(image, 'what is the style of this image')
1220 imagine_prompt = f"let's pretend you are an excellent painter and now " \
1221 f"there is an incomplete painting with {BLIP_caption} in the center, " \
1222 f"please imagine the complete painting and describe it" \
1223 f"you should consider the background color is {background_color}, the style is {style}" \
1224 f"You should make the painting as vivid and realistic as possible" \
1225 f"You can not use words like painting or picture" \
1226 f"and you should use no more than 50 words to describe it"
1227 caption = self.llm(imagine_prompt) if imagine else BLIP_caption
1228 caption = self.check_prompt(caption)
1229 print(f'BLIP observation: {BLIP_caption}, ChatGPT imagine to {caption}') if imagine else print(
1230 f'Prompt: {caption}')
1231 return caption
1232
1233 def resize_image(self, image, max_size=1000000, multiple=8):
1234 aspect_ratio = image.size[0] / image.size[1]
1235 new_width = int(math.sqrt(max_size * aspect_ratio))
1236 new_height = int(new_width / aspect_ratio)
1237 new_width, new_height = new_width - (new_width % multiple), new_height - (new_height % multiple)
1238 return image.resize((new_width, new_height))
1239
1240 def dowhile(self, original_img, tosize, expand_ratio, imagine, usr_prompt):
1241 old_img = original_img

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected