ai_image_ollama.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import logging
  2. from setting import ollama_client
  3. logger = logging.getLogger(__name__)
  4. model_name = 'moondream'
  5. def describe_frame(scene_list):
  6. results = []
  7. logger.info(f"🚀 开始分析视频,共有 {len(scene_list)} 个场景待处理...")
  8. for i, scene in enumerate(scene_list):
  9. logger.info(f"\n🎬 正在处理场景 {i + 1}/{len(scene_list)} (时间点: {scene['frame_pos']}s)")
  10. frame_path = scene['frame_path']
  11. with open(frame_path, 'rb') as f:
  12. image_bytes = f.read()
  13. image_data = {
  14. "scene_start": scene['scene_start'],
  15. "scene_end": scene['scene_end'],
  16. "frame_pos": scene['frame_pos'],
  17. "frame_path": frame_path,
  18. "prompts": []
  19. }
  20. prompts = [
  21. """
  22. Analyze the physical interaction between the individuals. Is there any intimate or sexual contact visible? Describe the positioning of their bodies and limbs objectively
  23. """,
  24. """
  25. Describe the clothing status of all individuals. Is there any visible nudity, undergarments, or partially exposed sensitive areas? Identify the specific body parts shown.
  26. """,
  27. """
  28. Observe the posture and movement. Does the scene depict a sexual act or a highly suggestive sexual position?provide a neutral description of the pose.
  29. """
  30. ]
  31. for i, p in enumerate(prompts):
  32. try:
  33. response = ollama_client.chat(
  34. model=model_name,
  35. messages=[{
  36. 'role': 'user',
  37. 'content': p,
  38. 'images': [image_bytes]
  39. }]
  40. )
  41. eng_text = response['message']['content']
  42. eng_text1 = eng_text.strip()
  43. prompt_item = {
  44. "prompt": p,
  45. "result": eng_text1
  46. }
  47. image_data["prompts"].append(prompt_item)
  48. except Exception as e:
  49. logger.error(f"❌ Prompt {i + 1} 推理失败: {e}")
  50. results.append(image_data)
  51. return results
  52. def describe_image(prompts, image_bytes):
  53. results = []
  54. for i, p in enumerate(prompts):
  55. response = ollama_client.chat(
  56. model=model_name,
  57. messages=[{
  58. 'role': 'user',
  59. 'content': p,
  60. 'images': [image_bytes]
  61. }]
  62. )
  63. eng_text = response['message']['content']
  64. prompt_item = {
  65. "prompt": p,
  66. "result": eng_text.strip()
  67. }
  68. results.append(prompt_item)
  69. return {
  70. "model_name": model_name,
  71. "results": results
  72. }