Python 快速入门
使用 Python 在 3 分钟内完成第一次 Annie Insights API 调用。
先决条件
- Python 3.8+
requests库(pip install requests)
完整示例
import requests
import os
import json
# Configuration
API_KEY = os.environ.get("ANNIE_API_KEY", "YOUR_API_KEY")
BASE_URL = "https://api.annie-insights.com/api/v2"
headers = {
"Content-Type": "application/json",
"x-api-key": API_KEY,
}
def analyze_clinical_image(patient_id: str, image_url: str) -> dict:
"""Analyze a dental image for clinical findings."""
payload = {
"PatientID": patient_id,
"TimeStamp": "2026-05-24",
"ImageName": image_url,
}
response = requests.post(
f"{BASE_URL}/clinical-images",
headers=headers,
json=payload,
)
response.raise_for_status()
return response.json()
def detect_ceph_landmarks(patient_id: str, image_url: str) -> dict:
"""Detect 169 lateral cephalometric landmarks."""
payload = {
"PatientID": patient_id,
"TimeStamp": "2026-05-24",
"ImageName": image_url,
}
response = requests.post(
f"{BASE_URL}/ceph-lateral-points",
headers=headers,
json=payload,
)
response.raise_for_status()
return response.json()
def estimate_treatment_length(**params) -> dict:
"""Predict orthodontic treatment duration."""
response = requests.get(
f"{BASE_URL}/tle",
headers={"x-api-key": API_KEY},
params=params,
)
response.raise_for_status()
return response.json()
def download_output_image(url: str, filename: str) -> None:
"""Download the annotated output image before SAS URL expires."""
response = requests.get(url, stream=True)
with open(filename, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Saved: {filename}")
# --- Usage ---
if __name__ == "__main__":
# 1. Clinical findings
result = analyze_clinical_image("P-001", "https://example.com/dental-photo.jpg")
print(json.dumps(result, indent=2))
# 2. Download the annotated output image
if "OutputImageUrl" in result:
download_output_image(result["OutputImageUrl"], "clinical_result.jpg")
# 3. Treatment length estimation
tle = estimate_treatment_length(
p01="14", p02="M", p03="ClassII", p04="Moderate",
p05="None", p06="5.2", p07="Deep", p08="None",
p09="None", p10="No", p11="3"
)
print(f"Estimated treatment: {tle['estimate_length']} months")
通过重试处理错误
import time
def call_with_retry(func, *args, max_retries=3, **kwargs):
for attempt in range(max_retries):
try:
return func(*args, **kwargs)
except requests.exceptions.HTTPError as e:
if e.response.status_code == 429:
wait = 2 ** attempt
print(f"Rate limited. Retrying in {wait}s...")
time.sleep(wait)
else:
raise
raise Exception("Max retries exceeded")
下一步
- C# 快速入门 — .NET 集成
- JavaScript 快速入门 — Node.js 集成
- API 端点 — 探索所有 13 个端点