Grok 4.1 Fast· xai
Frontier Reasoning Traces
Provide Python code to perform real-time face detection using OpenCV.
- User
Provide Python code to perform real-time face detection using OpenCV.
- Assistant
import cv2 # Load the pre-trained Haar Cascade classifier for frontal face detection face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # Initialize the video capture from the default webcam (index 0) cap = cv2.VideoCapture(0) # Check if the webcam is opened successfully if not cap.isOpened(): print("Error: Could not open webcam.") exit() print("Real-time face detection started. Press 'q' to quit.") while True: # Read a frame from the webcam ret, frame = cap.read() # If frame reading fails, break the loop if not ret: print("Error: Failed to read frame.") break # Convert the frame to grayscale for faster processing gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect faces in the grayscale frame # Parameters: scaleFactor=1.1, minNeighbors=5, minSize=(30,30) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) # Draw rectangles around detected faces for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # Blue rectangle, thickness 2 cv2.putText(frame, 'Face', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2) # Display the frame with detections cv2.imshow('Real-Time Face Detection', frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the video capture and close all windows cap.release() cv2.destroyAllWindows()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.