In this patent disclosure, a machine-learning-based system for automatically turning on/off a light source of an endoscope camera during a surgical procedure is disclosed. The disclosed system can receive a sequence of video images captured by the endoscope camera when the light source is turned on. The system next analyzes the sequence of video images using a machine-learning classifier to classify each video image as a first class of image of being inside the patient's body or a second class of image of being outside of the patient's body. The system next determines whether the endoscope camera is inside or outside of the patient's body based on the classified video images. When the endoscope camera is determined to be outside of the patient's body, the system generates a control signal for turning off the light source, wherein the control signal is used to immediately turn off the light source.